$ perl -le'$x="green"; for our $x ("red", "blue") { push @a, sub { $x +} } print $_->() for @a' green green $ perl -le'for my $x ("red", "blue") { push @a, sub { $x } } print $_- +>() for @a' red blue
Package variables never go out of scope. Each anon sub in the first command of the example therefore refers to the same variable, $main::x. A for loop with a package variable as its iterator will backup the value of the variable and restore it when the loop is exited to avoid clobbering its parent, so when the anon subs in the first command are executed, they display $main::x which was restored to 'green'.
On the other hand, a new my variable is created each time the scope that contains it is executed(*). That means that my variables that exist outside of a sub need to be captured by the sub to ensure that they are still around the sub is executed. For named subs, this is done when the sub is compiled. For anon subs, this is done when the sub expression is executed. Even if the variable goes out of scope, it will be kept alive and the sub will see the variable that existed when it was captured.
For example, even though $x went out of scope at the end of the file in the following code, set and get keep it alive and can use it to exchange data.
package Module; my $x; sub set { $x = shift } sub get { $x } 1;
use Module; Module::set(123); print Module::get(); # 123
When the anon subs in the second command of the top example are executed, they the current value of the variable they captured. One of those subs captured a variable that has 'red' for value. The other captured a variable that has 'blue' for value.
* — The implementation varies slightly due to optimisation.
In reply to Re: How do closures and variable scope (my,our,local) interact in perl?
by ikegami
in thread How do closures and variable scope (my,our,local) interact in perl?
by ELISHEVA
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |