Thanks, ikegami.

The documentation "The range operator (in list context) makes use of the magical auto-increment algorithm if the operands are strings." didn't quite shed any light on it, as I'm already familiar with the magical auto-increment feature.  (But I think hbm got it below, which clarified something that the documenation could possibly use better wording for).

As to the second, however, you say the workaround is to use a "modifiable copy":

for ('a'..'f') { my $letter = $_; # Make modifiable copy ... }

But why isn't the my that I'm using here:

foreach my $letter ('a' ... 'c', 'd' ... 'f') { print " $letter"; $letter = uc $letter; }

doing exactly that??

Update:  Actually, I see why. Of course, because it's assigning to a modifiable lvalue of the variable. Never mind.

But I'm still curious why this:

#!/usr/bin/perl -w use strict; use warnings; sub letters { foreach my $letter ('a' ... 'c') { print " $letter"; $letter = uc $letter; } print "\n"; } letters(); letters(); letters();

Doesn't exhibit the same behavior? Is it simply that it doesn't undergo the same optimization step you referred to?


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Re^2: Two Range Operator Anomalies by liverpole
in thread Two Range Operator Anomalies by liverpole

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.