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

Let me ask the question: what do you want?

next, if 1 .. 3; will only call next when the left condition is true until the right condition is true.

Written differently:

while (<>) { next, if 2 .. 5; print; }

Is sort of the same as:
my $index = 0; my $skip = 0; while (<>) { $index++; if ($skip or $index == 2) { if ($index == 5) { $skip = 0; } next; } print; }

If your code is looping over a sequence of numbers or if you are using another counter then you might be able to use: next if $_ == 2 .. $_ == 5;

Replies are listed 'Best First'.
Re^2: When doesn't the flip-flop operator work in all scalar contexts?
by siracusa (Friar) on Oct 02, 2008 at 14:28 UTC
    I'm not asking how to accomplish something. I'm just examining the dark corners of a particular operator, trying to determine if there are any bugs or "accidental features" lurking there.
Re^2: When doesn't the flip-flop operator work in all scalar contexts?
by JadeNB (Chaplain) on Oct 02, 2008 at 18:00 UTC
    I would have sworn that next, if test wouldn't work, but it does. I can't understand why from the documentation of the comma operator, though—it seems like it should try to evaluate next, then if test (which is of course a syntax error), but that doesn't happen. Instead it seems to compile the same with or without the comma.

      My take on it: The 'if' always follows a simple statement so the parser takes 'next,' as statement. Depending on context (in this case void?) this will be either a comma operator or a list with a dangling comma. It seems the comma operator also throws away dangling commas like a list:

      > perl -e ' $d= (1,2); print "$d\n";' 2 > perl -e ' $d= (1,2,); print "$d\n";' 2