In the perlop documentation, it says that once the left operand of the range operator '..' is true, the operator remains true until the right operand becomes true, after which the range operator becomes false again(though it doesn't actually become false until the next time the range operator is evaluated).

So I was toyin' around with the range operator for a little while. The following code produced the expected.

print "$_\n" for (1 .. 10);
But when I tried this...
print "$_\n" for (1 .. 0);
there was no output. I don't understand why this happened since I can't see why the left operand would not be true. I figured that the loop would continue infinitely, since the right operand would never be true because of the increment. So to see if the same thing would happen with string values, I tried this...
print "$_\n" for (z .. a);
Surprisingly, the output was a 'z'. At this point, I'm really confused. The operator is apparently not behaving the same for strings and integers. Not only that, the first time the operator is evaluated returns true, and then it becomes false after only one iteration.

It also says in the perlop docs that the left operand is not evaluated while the operator is in false mode, and the right operand is not evaluated while the operator is in true mode. If that is the case, how does the loop start and stop?

I know the external behavior of using something like (1 .. 10). That is, I understand what will happen. At this point, I'm really having fun looking at the internals of Perl. I'm lost on this one though.

Anyone feel like clearing this one up?

Amel - f.k.a. - kel


In reply to Range Operators Question by dsb

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.