smcdonald has asked for the wisdom of the Perl Monks concerning the following question:
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Scope surprise
by Nkuvu (Priest) on Apr 11, 2009 at 00:17 UTC | |
|
Re: Scope surprise
by AnomalousMonk (Archbishop) on Apr 11, 2009 at 01:14 UTC | |
|
Re: Scope surprise
by shmem (Chancellor) on Apr 11, 2009 at 07:19 UTC | |
|
Re: Scope surprise
by ikegami (Patriarch) on Apr 11, 2009 at 07:30 UTC | |
|
Re: Scope surprise
by bart (Canon) on Apr 11, 2009 at 18:11 UTC | |
by ikegami (Patriarch) on Apr 11, 2009 at 18:58 UTC | |
by bart (Canon) on Apr 11, 2009 at 20:23 UTC | |
by ikegami (Patriarch) on Apr 11, 2009 at 20:53 UTC |