in reply to Re^2: Closures & aliases
in thread Closures & aliases
There is a single variable called $x which is accessible from the main program and from within foo. The way it happens to be implemented internally is that they are treated as two separate variables that happen to be initially aliased to the same value. Here's a little ASCII diagram.my $x = 1; sub foo { $x }
Before the for loop; the two X's point to the same value
and during for $x (9) { ...$MAIN_X --- \ -> +---+ $FOO_X ------> | 1 | +---+
So calling foo from within the loop causes the 1 to be printed, not the 9.+---+ $MAIN_X -----> | 9 | +---+ +---+ $FOO_X ------> | 1 | +---+
You are mostly right about how localization and for aliasing work, expect that its not entire values that are temporarily saved, it is simply a C pointer, either in the symbol table (eg local $x), in the scratchpad (eg my $x; for $x ()), or in an array or hash slot (eg local $h{foo}), that is pushed onto perl's savestack and replaced with a pointer to a different value.
Dave.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Closures & aliases
by ambrus (Abbot) on Jun 07, 2004 at 19:40 UTC |