Sorry I disagree that this is a good example - "by the book" - of a closure, because it's far to magic.
Each closure has a new instance of $i which is mutable, looking at the code I would expect either always the same instance of $i or $i being an alias of an immutable literal... but it's neither of these...
Rolf, $i is a variable and it is always mutable. At the end of the closure (the loop) it goes out of scope, so there is no way to reference it directly. There are still references to $i in the subroutines that were generated inside the loop/closure. Via these subroutines you can do whatever you like to $i, including returning it (as reference, of course) and manipulating it outside of a subroutine.
Basically leaving a block only removes the symbol table entry for something. The object itself doesn't cease to exist until all references to it go away. This is very much by the book for Perl.
- doug
In reply to Re^3: Constant value in anonymous sub (closures & aliases)
by doug
in thread Constant value in anonymous sub
by Sewi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |