There's surely somebody out there, who wrote the code, and could explain it better... ;-)

Aliasing means, that the carcass of the variable is retained (its content saved to be restored later) and filled with a new structure, i.e. it gets a new pointer. Let's have a look. Loop with a symbol table variable:

perl -MDevel::Peek -le 'our $f = 42; Dump *f{SCALAR}; for $f (2) { Dum +p *f{SCALAR} }' SV = RV(0x8664670) at 0x863bc28 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x863c7bc SV = IV(0x8657eec) at 0x863c7bc REFCNT = 2 FLAGS = (IOK,pIOK) IV = 42 SV = RV(0x8664670) at 0x863be2c REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x863c7ec SV = IV(0x8657ee8) at 0x863c7ec REFCNT = 3 FLAGS = (PADBUSY,PADTMP,IOK,READONLY,pIOK) IV = 2

Note that the scalar variable (SV) contains the same reference value (RV(0x8664670)) outside and inside the loop. It's content however is allocated at a different memory location:

SV = RV(0x8664670) at 0x863bc28 # outside SV = RV(0x8664670) at 0x863be2c # inside

That's essentially the same for my variables.

perl -MDevel::Peek -le 'my $f = 42; Dump \$f; for $f (2) { Dump \$f } +for my $f (2) { Dump \$f }' SV = RV(0x9d1a678) at 0x9cf1c28 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x9cf1cdc SV = IV(0x9d0def8) at 0x9cf1cdc REFCNT = 2 FLAGS = (PADBUSY,PADMY,IOK,pIOK) IV = 42 SV = RV(0x9d1a678) at 0x9cf1e2c REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x9cf1c28 SV = IV(0x9d0defc) at 0x9cf1c28 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 2 SV = RV(0x9d1a678) at 0x9cf1c28 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x9cf1e2c SV = IV(0x9d0defc) at 0x9cf1e2c REFCNT = 1 FLAGS = (IOK,pIOK) IV = 2

Here we have three different storage locations, because the two loops have each its own scope:

SV = RV(0x9d1a678) at 0x9cf1c28 SV = RV(0x9d1a678) at 0x9cf1e2c SV = RV(0x9d1a678) at 0x9cf1c28

The RV(0x9d1a678) is the same, which shows us that the following for loops are aliasing the same $f :

my $f; for $f (1..3) { } for my $f (1..3) { }

In the perl sources, you'll find the relevant code which sets up the loop variable at Perl_newFOROP() in file op.c, and the "aliasing code" in pp_ctl.c at PP(pp_enteriter).

update: corrected code locations


In reply to Re^7: Hard syntax error or disambiguable parsing? by shmem
in thread Hard syntax error or disambiguable parsing? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.