Consider this code which actually changes the iterator variable instead of the element.

my @ar = (1, 2, 3);
for my $i (0 .. $#ar) {
  $i = $i + 1;
}

I understand you to be saying that in the quoted code, a change to $i does not affect the loop list element from which the value of $i is drawn, i.e, $i is not an alias.

But consider the failure of the following:

c:\@Work\Perl\monks>perl -wMstrict -le "print 'runtime'; for my $i (1, 2, 3) { $i = $i + 1; print $i; } print 'done'; " runtime Modification of a read-only value attempted at -e line 1.

I think that in both cases $i is being aliased to elements of an anonymous loop list.

In the first (quoted) case, the  (0 .. $#ar) expression creates a mutable anonymous list, the elements of which can be changed freely.

In the second case,  (1, 2, 3) creates an immutable loop list. In this case, the statement
    $i = $i + 1;
is equivalent to the meaningless statement
    1 = 1 + 1;
for the first element of the loop list because $i is aliased to a literal 1, and the interpreter throws up its hands.


Give a man a fish:  <%-(-(-(-<


In reply to Re^2: A specific term for a Perlism by AnomalousMonk
in thread A specific term for a Perlism by JupiterCrash

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.