in reply to my $var masked across package scope?
my declares a lexically scoped variable. A lexical scope continues until the closing brace (}) that ends the block it was defined in, or until the end of the file, whichever comes first.
{ package Foo; my $foo = 1; package Bar; # can still see $foo here package Baz; # can still see $foo here } # cannot see $foo any more
While our variables are associated with a particular package, they too are lexically scoped:
{ package Foo; our $foo = 1; # $foo is an alias for $Foo::foo package Bar; # $foo is still an alias for $Foo::foo here package Baz; # $foo is still an alias for $Foo::foo here } # cannot see $foo any more
For this reason, if you're defining multiple packages in the same file, it's a good idea to define them each within their own {...} block, so they don't accidentally leak variables. (Of course, sometimes - probably quite rarely - you'll actually want to share a lexical variable between the packages, in which case, just declare it right at the top before the first opening brace.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: my $var masked across package scope?
by QM (Parson) on May 10, 2013 at 08:13 UTC | |
by tobyink (Canon) on May 10, 2013 at 11:51 UTC | |
by Anonymous Monk on May 10, 2013 at 08:59 UTC |