Please enlighten me on what's going on with the use of my combined with an if statement modifier on the 4th line. The output is very surprising. I expected the output to be "1\n2\n".
The value of @a is being retained between invocations of fn(). But if the stack frame for fn() is destroyed between calls, where is the value of @a being kept? If my is not having the effect of creating a local due to the if, then why doesn't the compiler complain that @a is an undeclared global?
perl -we ' use strict; sub fn { my @a = (7) if 0; push @a, @_; print "@a\n"; } fn 1; fn 2; ' 1 1 2
In reply to Curious result of using "my ... if ..." by jrw
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |