in reply to Re: Lexical scoping like a fox
in thread Lexical scoping like a fox
Maybe it would be easier to express the distinction as spatial versus temporal scoping, which is an approach that I think makes sense at least until you start throwing threads and processes into the mix.
Let's take this example:
print "Just before start of scope\n"; { print "Just after start of scope\n"; my $a = 1; local $b = 1; print "a=$a, b=$b\n"; foo(); print "Just before end of scope\n"; } print "Just after end of scope\n";
Now, in this example, the line 'my $a = 1;' introduces a lexical variable. Any reference to $a in the statements following that introduction until the end of the scope refer to this newly introduced variable - and only those lines. This is a spatial scope: if the foo subroutine refers to a $a variable, it won't see this $a, because foo is not defined within this scope.
The line 'local $b = 1;' introduces a localisation - it tells the interpreter to save the current copy of the (package) variable $b, to be restored when execution reaches the end of the scope. Now it is a matter of time: after this localisation has been executed, anyone that looks at this package variable $b before the end of this scope is reached will see the localised version. If the foo subroutine refers to the package variable $b, it will see the localised version (for this call, at least).
Does that help at all?
Hugo
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Lexical scoping like a fox
by convenientstore (Pilgrim) on Jan 13, 2008 at 16:26 UTC | |
by shmem (Chancellor) on Jan 13, 2008 at 19:07 UTC | |
by graff (Chancellor) on Jan 13, 2008 at 17:58 UTC |