choroba has asked for the wisdom of the Perl Monks concerning the following question:
By user "timka7060":
Who can figure this out. I narrowed down my issue in some other code to this simple use case:# Can print $v only once!!! # Second call returns undef. # Some coderef optimization??? # Lexical variable goes undef on second call. # sub { my $v = 111; eval q( eval 'print $v, "\n"'; # 111 eval 'print $v, "\n"'; # undef ); }->();
User "tyrrminal" replies with:
If you enable warnings, you see a line Variable "$v" is not available at (eval 3) line 1. Googling that led me to a stack overflow response which seems to relate to your case (2nd answer, starting with See perldiag)
timka7060's reaction:
So why does the first eval output 111?
(I'd figure either both lines should be 111 or both undef.)
Btw, prior to 5.10, both lines would output 111: https://perlbanjo.com/b28dce8db2
This is a simple fix, but only for a single variable: https://perlbanjo.com/13dbb8d48d
My observation:
Now, none of either A or B sees $v, but C (obviously) does.sub { my $v = 111; eval q( eval 'print $v, "A\n"'; eval 'print $v, "B\n"'; print $v, "C\n"; ); }->();
What's going on here?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Lexical variable not available
by dave_the_m (Monsignor) on Mar 22, 2024 at 14:13 UTC | |
|
Re: Lexical variable not available
by dave_the_m (Monsignor) on Mar 18, 2024 at 23:54 UTC | |
|
Re: Lexical variable not available
by ikegami (Patriarch) on Mar 19, 2024 at 19:45 UTC | |
by ikegami (Patriarch) on Mar 19, 2024 at 19:57 UTC | |
|
Re: Lexical variable not available
by LanX (Saint) on Mar 18, 2024 at 21:34 UTC | |
|
Re: Lexical variable not available
by InfiniteSilence (Curate) on Mar 20, 2024 at 01:16 UTC | |
|
Re: Lexical variable not available
by Danny (Chaplain) on Mar 18, 2024 at 21:54 UTC | |
by choroba (Cardinal) on Mar 19, 2024 at 07:42 UTC | |
by LanX (Saint) on Mar 18, 2024 at 22:06 UTC |