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



John
-- 
use Perl;
program
fulfillment

-- 
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