in reply to Scope, package, and 'my' variables

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.

Replies are listed 'Best First'.
Re^2: Scope, package, and 'my' variables
by ff (Hermit) on Apr 30, 2005 at 13:59 UTC
    Thanks for the example. Minor quibble, perhaps it's my 5.8 system, but that second print statement does not fail as undefined. Instead it prints '1'.

    #!/usr/bin/perl -w use strict; my $x = 1; BEGIN { $x = 2; } print $x, "\n"; # 1 BEGIN { my $x = 1; } print $x, "\n"; # (sic: ) undefined, failure under strict.pm # Apr 30, 05: Actually, mine doesn't fail and prints '1' my $x = 1; # 'my' generates a 'masks declaration' warning { package foo; $x = 2; } print $x, "\n"; # 2 my $x = 1; # 'my' generates a 'masks declaration' warning { package foo; my $x = 2; } print $x, "\n"; # 1

      Each example must be run separately. You're getting the value of $x defined from the previous test. If you run the 2nd one by itself, however:

      $ perl -le ' BEGIN { my $x = 1; } print defined $x ? "defined" : "undefined";' undefined

      And with strict:

      $ perl -Mstrict -le ' BEGIN { my $x = 1; } print $x;' Global symbol "$x" requires explicit package name at -e line 2. Execution of -e aborted due to compilation errors.