Thank you for demonstrating this use of the range operator:
for ( @whatever ) {
if ( 1 .. 4 ) {
# enter this block during the first four iterations, then neve
+r again
}
}
It's documented in the perlop man page, but I had never seen it before, and when I first looked at your post, I thought "that can't be right -- how could that possibly work". But once I ran it myself, and studied the man page carefully, I saw the beauty of it, and I'm grateful for that.
Update: Having said that, it seems I'm still missing something. I stepped through the OP code with "perl -d", and sure enough, the "if ( 1 .. 4 )" worked as the OP says it should: the "if" block is entered on the first four iterations, then the "else" block is entered on the remaining iterations.
But when I tried the simplest possible snippet to do the same basic thing, it didn't work that way:
$_ = 0;
while ($_<6) {
$_++;
if ( 1 .. 4 ) { print "$_: True\n"; next; }
print "$_: False\n";
}
__OUTPUT__
1: False
2: False
3: False
4: False
5: False
6: False
When I study the man page again, this is sort of what I should have expected (but I think I should have gotten at least one "True" output):
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 can test the right operand and become false on the
same evaluation it became true (as in awk), but it still returns true once.
I get even more puzzled when I try this variation, which should evaluate to false on the first iteration (but doesn't -- and it doesn't flip-flop either):
$_ = 0
while ($_<6) {
$_++;
if ( 0 .. 3 ) { print "$_: True\n"; next; }
else { print "$_: False\n"; }
}
__OUTPUT__
1: True
2: True
3: True
4: True
5: True
6: True
What am I doing wrong here?