Closures are not relevant here:

#for (1..2) { # foreach my $x (0) { print $x++; } # Error! #} for (1..2) { foreach my $x (0..1) { print $x++; } # 01, 01 } for (1..2) { foreach my $x (0..1,5..6) { print $x++; } # 0156, 1267 }

I'm not sure why this only happens if you combine several ranges.

Actually, it also applies with just a single range:

for (1..2) { print map { $_++ } 0..1; # 01, 12 } for (1..2) { print map { $_++ } 0..1,5..6; # 0156, 1267 }

Keep in mind that foreach (expr1..expr2) is optimized into a counting loop, so $x is not an alias to a constant list in the case of foreach my $x (0..1).


You've apparently found an optimization. If you turn 0..1,5..6 into a non-constant expression, the expression rebuilds the list every time:

for (1..2) { foreach my $x (map $_, 0..1,5..6) { print $x++; } # 0156, 0156 }

Another fix is:

for (1..2) { foreach (0..1,5..6) { my $x = $_; print $x++; } # 0156, 0156 }

Personally I'd consider the fact that lists generated from ranges aren't read-only a bug.

Aye. Lists deemed constant for optimization purposes should have its values flagged read-only.

Update: Added a bit to make my explanations easier to follow. Adjusted my code to match the changes the OP did in his "Update 2".


In reply to Re: Unexpected behaviour with constant lists by ikegami
in thread Unexpected behavior of '..' lists by Crackers2

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.