in reply to Re: Understanding typeglobs and local()
in thread Understanding typeglobs and local()
I guess I'm trying to understand "how it works" on the inside so that I know what behaviour to expect. From what I understand, local ($_); is roughly equivalent to:
so foreach saved a pointer to the original $_, which contains 'Sample' as a result of s///. This I understand; it's why $alias1 is unaffected. Yet $alias0 is affected, and I'm not quite sure that I understand why. (This is no different than the local() case.)my $saved = $_; $_ = undef; ... $_ = $saved;
Inside the foreach, we alias to $x. This doesn't affect the preexisting aliases, but on the next iteration through the loop our alias still exists. The behaviour changes if the line *_ = *x; is changed to *_ = \$x;, and this is what I was trying to understand.
Hmm, maybe I'm starting to get it now. *x is a 'level higher' than \$x in the internal structure of the symbol table. So foreach is only localizing the scalar (and restoring it at exit - this changes what $_ is after the loop if that change is made), and changes to the typeglob take foreach() completely unawares.
Update: It turns out what was confusing me was that foreach() apparently makes its own copy of the glob for *_, as the alias *_ = *x doesn't change where foreach() puts its alias. *_ = \$x leaves the glob alone, and just modifies the scalar portion; this does get overwritten by foreach()'s aliasing on each iteration (and at loop exit) provided the glob has not been changed.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Understanding typeglobs and local()
by ikegami (Patriarch) on Jan 13, 2006 at 03:13 UTC | |
|
Re^3: Understanding typeglobs and local()
by ysth (Canon) on Jan 13, 2006 at 04:10 UTC | |
|
Re^3: Understanding typeglobs and local()
by dave_the_m (Monsignor) on Jan 13, 2006 at 12:25 UTC |