I figured it out! The range operator stuff freaked me out a little, but the big trick is that

foreach my $a (1 .. 5) { next while ($a != 2); }
won't go to the while it's next to, but the outer foreach loop. Thus, you don't enter an infinite loop.

A curious thing was that I wrote the quick program:

my @strings = ('a', 'b', 'c', 'd', 'e', 'f'); while (my $s = shift @strings) { print "Looking at $s\n"; next while ($s =~ /a/ .. $s =~ /c/); print "$s is OK!\n"; }
How does the range-op-boolean know that $s was a in a previous check. It seems to be maintaining state between invocations.

Update:

my @strings = ('a', 'b', 'c', 'd', 'e', 'f'); while (my $s = shift @strings) { print "Looking at $s\n"; my $value = $s =~ /a/ .. $s =~ /c/; print "The value is $value\n"; next if $value; print "$s is OK!\n"; }
This gives a very interesting output.

Looking at a The value is 1 Looking at b The value is 2 Looking at c The value is 3E0 Looking at d The value is d is OK! Looking at e The value is e is OK! Looking at f The value is f is OK!
I just find that '3E0' thing really odd...

In reply to Re: Re: This code is just freaky... by dragonchild
in thread This code is just freaky... by dragonchild

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.