Of course, re-my'ing the same variable isn't ideal, but the behavior surprised me. I would expect it to output "foo: 1" but it actually outputs "foo: ". It turns out that $foo is undef within the foo() function. Changing the second my:use strict; my $foo = 1; foo(); my $foo = 2; sub foo { print "foo: $foo\n"; }
to:my $foo = 2;
causes it to output "foo: 1" as expected. What's going on here? I see the same thing in perl 5.8.8 and 5.10.0.$foo = 2;
In reply to Scope surprise by smcdonald
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |