in reply to Re^4: 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?
Your code shows is that it prints the current value of $main::x, as set using = or by local restoring the backed up value.
I have no idea what that means. "Enclosed global" is a contradiction. Values aren't localised, variables are. (It backups up their value and restores it later.)it seems to me that an enclosed global does in fact get whatever happens to be the localized value of its current context.
when it runs in a block where $x is localized, it uses the localized value, not the value from the outer block.
Again, "localised value" makes no sense.
The "when" is misleading. There's nothing conditional about it. The same would apply if it wasn't localised. The whole sentence could be replaced with "It uses the current value".
I was thinking that localization "overlay" the variable with a new variable,
Localisation
But it's still the same variable. Any change to the variable will be seen globally.
$y = 123; sub f { print("$y\n"); } # 456 { local $y = 456; f(); } # Same var
That differs from my which actually creates a new var.
my $x = 123; sub f { print("$x\n"); } # 123 { my $x = 456; f(); } # Different var
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: How do closures and variable scope (my,our,local) interact in perl?
by shmem (Chancellor) on Jun 16, 2009 at 21:29 UTC |