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

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...

Replies are listed 'Best First'.
Re: Re: Re: This code is just freaky...
by John M. Dlugosz (Monsignor) on Jul 27, 2001 at 22:47 UTC
    I love the flip-flop operator. The description in older Camel books is classic. Its befuddling until you get through the whole thing, and great to impress other (non-Perl) programmers with.

    The E0 suffix thing is a bit clunky. Perl 6 should do that better, using properties.

    —John

Re: Re: Re: This code is just freaky...
by petral (Curate) on Jul 29, 2001 at 12:41 UTC
    '3E0' is a variation on "0 but true". You can discover that it's the last line by matching /E0$/
    (efficiency:  index $value,'E0' >= 0), or treat it as a number, which yields 3.

      p