sub make_closure { my ($x) = @_; return sub { $x }; } my $c1 = make_closure("a"); my $c2 = make_closure("b"); say $c1->(); # a say $c2->(); # b
The $x from the first call to make_closure and the $x from the second call to make_closure are kept alive beyond the end of their normal scope (the end of that call to make_closure).
In reply to Re^5: Accessing lexicals in other scopes dynamically by name
by ikegami
in thread Accessing lexicals in other scopes dynamically by name
by LanX
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |