What's going on here?
The second my $foo masks the first; you know that. So the compiler allots different slots for them. Now, the sub foo happens to be in the same scope as that second my $foo, but at the time you call that function, it has not gotten a value assigned, so it is undef.
use Devel::Peek; use strict; $|=1; my $foo = 1; print "1st foo: "; Dump($foo); foo(); my $foo = 2; print "2nd foo: "; Dump($foo); sub foo { print "sub foo: "; Dump($foo); print "foo: $foo\n"; } __END__ 1st foo: SV = IV(0x99b1c8c) at 0x9996768 REFCNT = 1 FLAGS = (PADBUSY,PADMY,IOK,pIOK) IV = 1 sub foo: SV = NULL(0x0) at 0x9996a8c REFCNT = 2 FLAGS = (PADBUSY,PADMY) foo: 2nd foo: SV = PVIV(0x9997b10) at 0x9996a8c REFCNT = 2 FLAGS = (PADBUSY,PADMY,IOK,pIOK) IV = 2 PV = 0
The second my $foo is allocated at 0x9996a8c, and that's the one seen in the sub.
In reply to Re: Scope surprise
by shmem
in thread Scope surprise
by smcdonald
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |