Perl: the Markov chain saw | |
PerlMonks |
Re^4: Closure confusion in Tkx (declarations in loops - inner vs outer)by LanX (Saint) |
on Jul 13, 2019 at 21:35 UTC ( [id://11102804]=note: print w/replies, xml ) | Need Help?? |
> as to why the c-style for fails in this case. Think of lexicals in a scope like having a different references each time the scope is passed at runtime. But - like already demonstrated - the first part of the c-style for is outside the block to be implemented as a while.
More gory details?A lexical declaration in a scope reserves - at run time - a storage place in memory which is released at the end of scope. No release happens if the ref counter isn't 0, like when the lexical is used in a closure. So if the last lexical was released it might happen that the ref is reused, but it's better to assume they aren't the same variable.
$x has different refs in line 9 because the first $x is captured in a closure and not relased. Hence Perl can't reuse the reference. HTH! :) Cheers Rolf
In Section
Seekers of Perl Wisdom
|
|