in reply to State information for flipflop .. operator.

You can keep track of it's state by storing its return value. If the last return value was false or =~/E/, it will look for the first regex on the next iteration; otherwise it will look for the second.

A quick test:

$ perl -we'sub makeit { my $i; sub { ++$i; ($i%6==1)..($i%6==3) } } $z = makeit(); $y = makeit(); print grep {&$y; &$z} "a".."r"' abcghimno
shows that multiple closures made from the same anonymous sub have different states. The &$y call has no effect on &$z's return value.

It is likely to be possible to save (and with 5.8.1, restore) a flipflop's state using the B module.

Replies are listed 'Best First'.
Re^2: State information for flipflop .. operator.
by rinceWind (Monsignor) on Aug 16, 2004 at 15:58 UTC
    A quick test ... shows that multiple closures made from the same anonymous sub have different states
    Nice one! That's what I wanted to hear.
    It is likely to be possible to save (and with 5.8.1, restore) a flipflop's state using the B module.
    I don't think my internals knowledge is up to it. A CPAN module that did this would be nice though :).

    In the mean time I am looking at preserving the input file position (with tell), together with the line counter and flipflop state using the return value from .. (in combination with $. when .. returns false). When I restore, I will need to short circuit the left operand if the flipflop state was on.

    Anyone see any issues with this?

    --
    I'm Not Just Another Perl Hacker