in reply to Closures and scope of lexicals
In the for my ... case, the loop lexical is implicitly scoped to inside the for body. At the point it reaches the end of the block, the lexical goes out of scope and perl notes that there's an external reference to it and allocates a new one. (This is true for all lexicals and blocks, not specific to for.)
In the second case, the lexical never goes out of scope, so all closures reference the same scalar, just as if you did push @subrefs, sub { print $i }, sub { print $i }; in the "good" code it would produce 112233.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Closures and scope of lexicals
by haj (Vicar) on Oct 30, 2024 at 20:54 UTC | |
by jdporter (Paladin) on Oct 31, 2024 at 13:03 UTC | |
by LanX (Saint) on Oct 31, 2024 at 03:00 UTC |