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.)
In reply to Re: my $var masked across package scope?
by tobyink
in thread my $var masked across package scope?
by QM
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |