Others have explained this behavior by pointing out that lexical variables are scoped to the current block. That's right, but you might be wondering how that determination is made. I like to think of it as an entirely physical process. It doesn't matter what abstract delineations occur (e.g., different packages, BEGIN blocks, END blocks), the physical delineations are what matter for lexical variables. Just to drive the point home:
my $x = 1; BEGIN { $x = 2; } print $x; # 1
BEGIN { my $x = 1; } print $x; # undefined, failure under strict.pm
my $x = 1; { package foo; $x = 2; } print $x; # 2
my $x = 1; { package foo; my $x = 2; } print $x; # 1
Hopefully these examples help you understand the point I'm trying to make. That is, the runtime order of the code doesn't matter to lexical variables, nor does the abstract boundaries of packages. The only thing that matters is where in the source code the variables are scoped and used.
In reply to Re: Scope, package, and 'my' variables
by revdiablo
in thread Scope, package, and 'my' variables
by ff
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |