more useful options | |
PerlMonks |
Re: i don't understand this scope behavior w/ closures and evalby crashtest (Curate) |
on Feb 11, 2010 at 22:33 UTC ( [id://822773]=note: print w/replies, xml ) | Need Help?? |
Why turn warnings off? Turning them on (and autoflushing STDOUT and STDERR, so the output is in order) prints:
In the first factory, you're using $var in an eval{} block... which is quite different from a string eval(). This isn't an interesting case, and works as you expected anyway. The difference between your second and third examples is the supposedly useless (as perl tells you) statement $var. The compressed answer is that when you generate the closure in these latter cases, perl has no idea that you're making use of $var in your block, and so it sees no reason to include it in the closure. After all, all perl sees is a string and some function call (to eval()). But when you explicitly make use of $var in your (third) factory, perl includes it in the closure - not because it knows you're going to need it, but because you included it. When the string eval is then evaluated, it finds $var in the third factory, but can't resolve it in the second factory. To make sure it is clear what I am referencing from a closure, I always like to explicitly write out which variables I need for it... I'd write something like:
In Section
Seekers of Perl Wisdom
|
|