It appears that perl does not see the use of $x in foo, and thus does not close foo on $x. But once you put in the "print $x" there, it does close foo on $x, and then the anonymous sub can close on the same $x.
One workaround would be:
Note the useless use of $x in a void context, which gets masked away with the no warnings bit. It does the same as yours as far as the problem is concerned (forcing perl to close foo on $x), but with no side effects going to standard-out ;-)use strict; use warnings; sub run_this { shift->() } { my $x = "hello\n"; sub foo { { no warnings; $x } run_this( sub { print $x; }); } } foo();
In reply to Re: Doubly-nested deeply bound variable is undefined
by Tanktalus
in thread Doubly-nested deeply bound variable is undefined
by asokoloski
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |