in reply to Re^2: Constant value in anonymous sub (closures & aliases)
in thread Constant value in anonymous sub
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Constant value in anonymous sub (closures & aliases)
by LanX (Saint) on Feb 19, 2010 at 20:29 UTC | |
by doug (Pilgrim) on Feb 23, 2010 at 14:27 UTC | |
by LanX (Saint) on Feb 24, 2010 at 14:56 UTC |