I'm dismayed. OK, so this was probably the most fun Perl bug I've ever found. Even today I'm learning new stuff about Perl. Essentially, the flip-flop operator ..
which returns false until the left-hand-side returns true, and then true until the right-hand-side returns false keep global state (or that is what I assume.)
Can I reset it (perhaps this would be a good addition to Perl 4-esque hardly ever used reset()
)? Or, is there no way to use this operator safely?
I also don't see this (the global context bit) documented anywhere in perldoc perlop
is this a mistake?
Code
use feature ':5.10';
use strict;
use warnings;
sub search {
my $arr = shift;
grep { !( /start/ .. /never_exist/ ) } @$arr;
}
my @foo = qw/foo bar start baz end quz quz/;
my @bar = qw/foo bar start baz end quz quz/;
say 'first shot - foo';
say for search \@foo;
say 'second shot - bar';
say for search \@bar;
Spoiler
$ perl test.pl
first shot
foo
bar
second shot
A workaround/hack/cheat for your particular case is to append the end value to your array:
This will guarantee that the RHS of range operator will eventually be true.
Of course, this is in no way a general solution.
In my opinion, this behavior is not clearly documented. If you can construct a clear explanation, you could apply a patch to
perlop.pod
viaperlbug
.The "range operator"
..
is documented in perlop under "Range Operators". Looking through the doucmentation, it appears that there isn't any way to reset the state of the..
operator. Each instance of the..
operator keeps its own state, which means there isn't any way to refer to the state of any particular..
operator.It looks like it's designed for very small scripts such as:
The documentation states that this is short for
Somehow the use of
$.
is implicit there (toolic points out in a comment that that's documented too). The idea seems to be that this loop runs once (until$. == 200
) in a given instance of the Perl interpreter, and therefore you don't need to worry about resetting the state of the..
flip-flop.This operator doesn't seem too useful in a more general reusable context, for the reasons you've identified.
Can someone clarify what the issue with the documentation is? It clearly indicates:
There is some vagueness there about what "Each" means, but I don't think the documentation would be well served by a complex explanation.
Note that Perl's other iterators (
each
or scalar contextglob
) can lead to the same problems. Because the state foreach
is bound to a particular hash, not a particular bit of code,each
can be reset by calling (even in void context)keys
on the hash. But forglob
or..
, there is no reset mechanism available except by calling the iterator until it is reset. A sample glob bug:For the overly curious, here are some examples where the same .. in the source is a different .. operator:
Separate closures:
Comment out the
$x if 0
line to see that non-closures have a single .. operation shared by all "copies", with the output being12
.Threads:
Threaded code starts with whatever the state of the .. had been before thread creation, but changes to its state in the thread are isolated from affecting anything else.
Recursion:
Each depth of recursion is a separate .. operator.
I found this problem, and as far as I know there's no way to fix it. The upshot is - don't use the
..
operator in functions, unless you are sure you are leaving it in the false state when you leave the function, otherwise the function may return different output for the same input (or exhibit different behaviour for the same input).The trick is not use the same flip-flop so you have no state to worry about. Just make a generator function to give you a new subroutine with a new flip-flop that you only use once:
I also write about this in Make exclusive flip-flop operators.
Each use of the
..
operator maintains its own state. Like Alex Brown said, you need to leave it in the false state when you leave the function. Maybe you could do something like: