a "localised" lexvar my var;for $var(){} has the same "closuring"-effects like a for my $var () {} .
A capture is really a form of aliasing. A slot in the new sub's pad is aliased to the SV of the captured variable. Or three, if another variable already refers to that SV.
$ perl -MDevel::Peek -e' my $foo = "foo"; my $x; Dump($x); Dump($foo); for $x ($foo) { Dump($x); $f = sub { Dump($x) } } $f->() ' SV = NULL(0x0) at 0x814f69c SV = PV(0x814fb00) at 0x814ecdc SV = PV(0x814fb00) at 0x814ecdc SV = PV(0x814fb00) at 0x814ecdc
All three names ($foo, loop's $x and sub's $x) all reference (are all aliased to) the same SV (0x814ecdc).
In reply to Re^2: 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: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |