Subs only capture the vars they think they need. They get it wrong in the case sub { eval '$x' }.
In older versions of Perl, the error was silent.
$ perl -wle' sub make { my ($x) = @_; return sub { eval q{$x}; }; } print make(123)->() ' Use of uninitialized value in print at -e line 9.
In newer versions of Perl, the error still exists, but a warning issued.
$ perl -wle' sub make { my ($x) = @_; return sub { eval q{$x}; }; } print make(123)->() ' Variable "$x" is not available at (eval 1) line 2. Use of uninitialized value in print at -e line 9.
The solution is to add a reference to the variable that needs to be captured inside of the anon sub.
$ perl -wle' sub make { my ($x) = @_; return sub { $x if 0; # <--- added eval q{$x}; }; } print make(123)->() ' 123
In reply to Re: Tracking down the source of a new 5.10 warning
by ikegami
in thread Tracking down the source of a new 5.10 warning
by mpeters
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |