Lexical variables are determined at compile time:
# file Foo.pm
use strict;
package Foo.pm
my $bee = 2; # lexical variable
sub foo {
return $bee++;
}
Assuming that's all in that file, Foo::foo() is the only subroutine with a handle on the lexical $bee. Note that foo() even keeps that variable available when $bee goes "out of scope". this is very important in understanding what's really going on. You'll want to do a search on "perl closures".
That means there is no way to get at $bee except via Foo::foo(). Say:
# some other file Bar.pm
use strict;
package Bar;
sub bar {
return $bee;
}
That fails because there is no $Bar::bee variable, but also, there is no way to specify you actually meant the $bee variable bound to the Foo::foo function.
Note that package (or "global") variables can be accessed from other scopes:
# file Foo.pm
package Foo;
our $blah = 2; # or use vars
#file Bar.pm
package Bar;
print $Foo::blah;
|