in reply to Re^3: How do closures and variable scope (my,our,local) interact in perl?
in thread How do closures and variable scope (my,our,local) interact in perl?
Perhaps we are just going in word circles here, but it seems to me that an enclosed global does in fact get whatever happens to be the localized value of its current context. In this example below, the sub is defined in the outer block, but when it runs in a block where $x is localized, it uses the localized value, not the value from the outer block.
my @colors=qw(red blue green yellow orange purple violet); print "----- our \$x; for \$x ...; after for block ---------\n"; our $x; foreach $x (@colors) { no strict 'refs'; my $name=$x; *{$x.'_0'}=sub { $x="$x|$x"; print "doubling \$x: <$x>\n"; }; } print "setting \$x to 'x'\n"; $x="x"; red_0(); { local $x="X"; print "localized \$x to 'X'\n"; red_0(); print "leaving localization block\n"; } red_0();
prints out
----- our $x; for $x ...; after for block --------- setting $x to 'x' doubling $x: <x|x> localized $x to 'X' doubling $x: <X|X> leaving localization block doubling $x: <x|x|x|x>
But maybe what you are trying to say is that the way I talked about "localization" indicates a misunderstanding? I was thinking that localization "overlay" the variable with a new variable, but it seems that you are saying that it doesn't do that at all: rather it shoves the current value off into a side area, sets a new value, and the copies the old value back at the end of the block. Do I have that right?
As for the troubling difference, I suppose I think of file scope my variables as the "same kind" of variable as "our" variables, just private to a file. My guess is that you do not - for one - there are some major differences: our variables can be localized; my variables, even file scope my variables, cannot.
All the same, outside of the for loop I expect anything associated with file scope $x to have the value assigned to file scope $x. But that is not what happens. Consider what happens if we use my $x; for $x.... In this case, running the sub after the for block is not affected at *all* by the current value of $x, even though $x was declared as a file scope lexical. It does, however, make and pick up changes to @colors as this code and output shows:
my @colors=qw(red blue green yellow orange purple violet); print "----- my \$x; for \$x ... ---------\n"; my $x; foreach $x (@colors) { no strict 'refs'; my $name=$x; *{$x.'_0'}=sub { $x="$x|$x"; print "doubling \$x: <$x>\n"; }; } $x="x"; print "set \$x to 'x'\n"; print "\$colors[0]=<$colors[0]> \$x=<$x>\n"; red_0(); print "\$colors[0]=<$colors[0]> \$x=<$x>\n"; $colors[0]='RED'; print "setting \$colors[0] to 'RED'\n"; red_0();
outputs
----- my $x; for $x ... --------- set $x to 'x' $colors[0]=<red> $x=<x> doubling $x: <red|red> $colors[0]=<red|red> $x=<x> setting $colors[0] to 'RED' doubling $x: <RED|RED> doubling $x: <RED|RED|RED|RED>
In other words, when captured by a closure, the our variable loses its aliased association outside of the for loop, the file scope my variable does not [ inside the closure ].
Best, beth
Update: clarification in square brackets. As ikegami point out below, my $x not in the closure does in fact lose its aliasing.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: How do closures and variable scope (my,our,local) interact in perl?
by ikegami (Patriarch) on Jun 16, 2009 at 19:43 UTC | |
by shmem (Chancellor) on Jun 16, 2009 at 21:29 UTC | |
|
Re^5: How do closures and variable scope (my,our,local) interact in perl?
by ikegami (Patriarch) on Jun 16, 2009 at 20:03 UTC |