John W. Krahn wrote:
Randy W. Sims wrote:
Roman Daszczyszak wrote:
In my perl script, I have a "global" variable called
@excludedIPAddresses, declared at the top of the script using my:
[...]
When I run this, I get an error "Can't localize lexical variable".  I
understand that it's because the variable is declared using "my"; what
I don't understand is why, or what I should declare the variable as,
since if I leave out "my" I get an error using "use strict".
The C<my> and C<our> declarators have less to do with scope than with
storage and access.

Variables declared with C<my> are limited to the scope in which they are
declared and any inner scopes, and are known as "local" or "lexical"
variables. They may be re-declared within an inner scope, in which case
the inner scope masks the outer, but the outer variable still exists, it
retains its value, and it will be accessible again when control returns
to the outer scope. For C<my> variables, storage is tied to the scope in
which the variable is declared, in a "scratchpad" that is thrown away at
the end of the scope. C<my> variables can also be global variables when
declared in a large scope.

Variables declared with C<our> are tied to a package and are stored as
part of the package, and are known as "package" variables. They are
accessible anywhere in the package by using the short name or outside
the package by using the fully qualified name (i.e.
<type-glyph><package-name>::<variable-name>).

Variables declared with our() have the same scoping rules as variables
declared with my().

$ perl -le'
use warnings;
use strict;

package me;
our $x = q/our/;
my  $y = q/my/;

package main;
print for $x, $y;
'
our
my

Sort of. This is one of Perl's weird features. I'm not quite sure how to define the behavior...

The C<my> variable is scoped from the point of declaration to the end of the enclosing scope, which, in this case, is file scope. This is not unusual.

The C<our> declaration creates the package variable $me::x. If you add to the end of your example:

use Data::Dumper;
print Dumper \%me::;

You'll notice that the symbol 'x' has been created as an alias for *me::x. So with C<our> we are still talking about a package variable. What I'm unsure of is how the alias is set up. There is no symbol 'x' in main:

print Dumper \%::

Both $x and $y are only valid from the point they are declared until the end of the file. If package 'me' were defined in a different "file", both $x and $y would not be accessible from package 'main'.

So I guess I don't really know technically how $x is resolved to $me::x from within main. Maybe it's quietly added to the lexical scratchpad for the file scope as an alias to the package symbol of the same name? If that is the case, then C<our> and C<my> would always share the same scoping, but have different storage and access semantics.

Randy.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to