It took me awhile to figure it out, but it was enlightening about the workings of "my":$a=1; $b=2; sub { my (0?$a:$b) = 3; print "a=$a\nb=$b\n" }->()
For some reason, line A creates pad entries for both $a and $b; note that the ternary test must be a constant or it won't compile. Line B, of course, only initializes $b. This means print sees the lexical $a and $b (not the global $a), but only $b is initialized, so the program outputs:my (0?$a:$b) = 3; # becomes BEGIN { create_lexicals(0?$a:$b); } # A (0?$a:$b) = 3; # B
As an added bonus, B::Deparse gets it wrong:a= b=3
$a = 1; $b = 2; sub { my($b) = 3; print "a=$a\nb=$b\n"; } ->();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: my (0?$a:$b): a koan
by wind (Priest) on May 04, 2011 at 19:11 UTC | |
by educated_foo (Vicar) on May 04, 2011 at 19:46 UTC | |
by LanX (Saint) on May 04, 2011 at 20:10 UTC | |
by educated_foo (Vicar) on May 04, 2011 at 21:13 UTC | |
by wind (Priest) on May 04, 2011 at 21:24 UTC | |
by BrowserUk (Patriarch) on May 04, 2011 at 20:00 UTC | |
|
Re: my (0?$a:$b): a koan
by ikegami (Patriarch) on May 05, 2011 at 06:25 UTC | |
|
Re: my (0?$a:$b): a koan
by BrowserUk (Patriarch) on May 04, 2011 at 16:58 UTC | |
by educated_foo (Vicar) on May 04, 2011 at 17:04 UTC | |
|
Re: my (0?$a:$b): a koan
by JavaFan (Canon) on May 05, 2011 at 09:47 UTC | |
by educated_foo (Vicar) on May 05, 2011 at 14:42 UTC | |
by ikegami (Patriarch) on May 05, 2011 at 16:26 UTC | |
by JavaFan (Canon) on May 05, 2011 at 16:58 UTC | |
by ikegami (Patriarch) on May 05, 2011 at 18:16 UTC | |
|