What, exactly, does 'aliasing that $v' mean?

This may not be what you're asking, but... the mechanics of "aliasing" fall out of the way Perl manages values.

The name $s refers to a pointer to the value currently associated with that name. The array entry for $a[78] also refers to a pointer to the value currently associated with the entry. And so on. All values are accessed via one level of indirection.

To make $v into an alias for some array entry, Perl simply copies the array entry's pointer to $v's pointer... shazzam ! (Which would probably be curtains for the old value associated with $v, "garbage collection"-wise; but that's another story.)

Problem is clearly that on loop exit, left to its own devices, the loop variable would remain an alias -- aliases are magic enough when contained within the scope of the loop !

I now speculate: I suspect that the $v doesn't know it's an alias, the mechanism is so deeply buried in the way variables work -- but in any case, extra code would be required to copy the last value to $v. Besides, much of the time it's useful to have a local loop variable, and since that mechanism already existed my guess would be that looked like the simplest approach...

What is the significance of it using that variable? When does it make a difference?

The difference is that if there is a Global and a Lexical $i in existence at the same time, then it is the Lexical that is localized for the duration of the loop. As this illustrates:

use strict ; use warnings ; use vars '$i' ; $i = 'hello there' ; for $i (1..2) { print "$i: $main::i\n" ; # 1: 1 } ; # 2: 2 print "$i\n" ; # hello there for my $i (4..5) { print "$i: $main::i\n" ; # 4: hello there } ; # 5: hello there print "$i\n" ; # hello there my $i = '*' ; for $i (7..8) { print "$i: $main::i\n" ; # 7: hello there } ; # 8: hello there print "$i: $main::i\n" ; # *: hello there
I suppose it might have been clearer if my at the package layer declared package variables... but that horse left home at some speed many, many moons ago.


In reply to Re^7: Hard syntax error or disambiguable parsing? by gone2015
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.