Think about Loose Coupling | |
PerlMonks |
Unpredicted late destructionby ferrency (Deacon) |
on Nov 05, 2003 at 16:31 UTC ( [id://304765]=perlquestion: print w/replies, xml ) | Need Help?? |
ferrency has asked for the wisdom of the Perl Monks concerning the following question:
I just came across a behavior in Perl (5.8.0) which I did not predict. When calling a function inside a foreach statement's LIST, any lexicals declared inside that function were not destroyed when I expected them to be destroyed.
Here is a minimal example which demonstrates what I mean: This warns no lock 2 because the lexical $F inside fn() is not destroyed until after the foreach loop is finished. Does anyone have a good explanation for why this makes sense? A more complete example is below...
In this example, it warns no lock 3 and no lock 4, but the rest of the cases destroy fn()'s lexical variables when I'd expect.
Thanks for any light you can shed on this one!
And, to demonstrate that it's not limited to the case of implicitly closing filehandles:
This prints I fot FOO before DESTROYING FOO.
Alan
Back to
Seekers of Perl Wisdom
|
|