I've run into an interesting problem with closures. Here's my minimal test case:
This compiles just fine, but if you run it $x doesn't print and an "uninitialized value" warning is generated. However, if you uncomment the first "print" statement, it works! $x is printed correctly both times.use strict; use warnings; sub run_this { shift->() } { my $x = "hello\n"; sub foo { # print $x; run_this(sub { print $x; }); } } foo();
Edit: turns out to be a bug in perl, fixed in 5.9.3. Thanks monks!
My best guess is that perl thinks that $x is not used within foo(), so it doesn't bother to keep track of it -- unless you reference $x directly within foo(). Maybe it doesn't look into the inner sub or something.
In any case, it seems to me that perl should either complain about $x in the anonymous sub at compile time, or it should just work. But I may be completely off track here.
Any ideas? Thanks!
asokoloski
In reply to Doubly-nested deeply bound variable is undefined by asokoloski
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |