Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re: Closure Explanationby almut (Canon) |
on Apr 30, 2010 at 06:22 UTC ( [id://837701]=note: print w/replies, xml ) | Need Help?? |
Just think of it this way: when a function closes over a lexical variable, that instance of the variable (and its associated storage location) won't be freed, as it normally would when the variable goes out of scope — i.e. it continues to remain accessible to the function. Note that you can (in theory) have more than one closure sharing the same variable instance (kind of their private global variable). Extending the Anonymous Monk's code:
would give
As you can see by looking at the addresses, each pair of closures shares one variable, so the respective value is incremented every time one of the two counter functions is called.
In Section
Seekers of Perl Wisdom
|
|