in reply to Multi-stage flip-flop?

"Each ".." operator maintains its own boolean state, even across calls to a subroutine that contains it." (source) How would two independent operators know they are "linked"? What are the scoping rules for this? Sounds too magical for me. Probably easier to just stick to a classic state machine design...

Replies are listed 'Best First'.
Re^2: Multi-stage flip-flop?
by runrig (Abbot) on Dec 12, 2014 at 23:57 UTC
    If you use closures, you can force a different internal state for each range operator, e.g.:
    sub mkrange { my ($r1, $r2) = @_; return sub { /$r1/../$r2/ }; } my $range1 = mkrange(qr/FIRST/, qr/SECOND/); my $range2 = mkrange(qr/THIRD/, qr/FOURTH/); while (<DATA>) { if ( $range1->() ) { print "R1: $_"; } if ( $range2->() ) { print "R2: $_"; } } __END__ junk FIRST 1 2 3 SECOND junk THIRD 4 5 6 FOURTH junk
Re^2: Multi-stage flip-flop?
by RonW (Parson) on Dec 11, 2014 at 01:47 UTC

    Within the same block, .. conditionX associates with the nearest condition1 .. condition2 lexically above it. So in:

    if (cond1 .. cond2) { if ( .. condA) { } } if ( .. cond3) { } if (cond4 .. cond5) { if (condB .. condC) } if ( .. cond6) { }

    .. cond3 would associate with cond1 .. cond2

    .. condA would not, because its in a different block (it would be an orphan)

    .. cond6 would associate with cond4 .. cond5 (it would not "see" condB .. condC because that is in a different block)

    In terms of parsing, the handler for .. conditionX would search backwards until it found a conditionV .. conditionW or the beginning of the block. Then the conditionV .. conditionW would be "informed" of the .. conditionX

    (Updated to add paragraph about parsing)