Why doesn't the flip-flop operator work in all scalar contexts? It works fine when looping over a file handle:

while(<$fh>) { next if 1..1; # skip first line print; }

But it doesn't seem to work at all in other kinds of loops:

# intentionally not using range operator to avoid confusion for(1,2,3,4,5) { next if 1..1; # never returns true! print "$_\n"; }
my $i = 0; while(++$i <= 5) { next if 1..1; # never returns true! print "$i\n"; }

Both of those examples print the numbers 1 through 5, rather than skipping 1 and just printing 2 through 5.

The docs for perl 5.10 don't seem to indicate that it only works in the context of file handles. From perldoc perlop:

In scalar context, ".." returns a boolean value. The operator is bistable, like a flip-flop, and emulates the line-range (comma) operator of sed, awk, and various editors. Each ".." operator maintains its own boolean state. It is false as long as its left operand is false. Once the left operand is true, the range operator stays true until the right operand is true, AFTER which the range operator becomes false again. It doesn't become false till the next time the range operator is evaluated.

It just says "in scalar context," not "in scalar context if and only if you're looping over a file handle's records." Bad docs? Bad implementation? What am I missing?


In reply to Why doesn't the flip-flop operator work in all scalar contexts? by siracusa

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.