Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Flip-flop reset?

by ysth (Canon)
on Aug 30, 2006 at 21:56 UTC ( [id://570490]=note: print w/replies, xml ) Need Help??


in reply to Flip-flop reset?

my @arr = ...; sub mySub { goto &{ sub { # added line foreach ( @arr ) { if ( /^PIDTAB/ ... /^\n/ ) { next if ( /^(PIDTAB|\n)/ ); while ( /\s+(\d+):\s+(\d+)\s*:\S+:\S+:D\s+/g ) { # do something } } } }} # added line }
will do the trick, since the sub {} will get a new pad (where the .. state is stored) each time mySub is called. This only works if the sub is a closure (uses an outer lexical); otherwise, the sub{} is treated more as if it were a kind of constant, and doesn't get a new pad each time the reference is returned by sub. If @arr isn't lexical, you can still force a closure by replacing
goto &{ sub {
with
my $dummy; goto &{ sub { $dummy if 0;

Replies are listed 'Best First'.
Re^2: Flip-flop reset?
by runrig (Abbot) on May 31, 2007 at 23:12 UTC
    I was recently wondering how to do this, and why a regular anonymous sub (w/o closures) wouldn't work. Then I found this node. Though my solution (before finding this) was to generate an anonymous sub using eval $string. E.g.
    my $range_checker = eval 'sub { local $_ = shift; /foo/../bar/ }';
    At first, your way seems a little too indirect and magical, but it makes perfect sense if you create a 'flip-flop generator':
    sub mk_flip_flop { my ( $re1, $re2 ) = @_; sub { local $_ = shift; /$re1/../$re2/ } }
Re^2: Flip-flop reset?
by ysth (Canon) on Aug 31, 2006 at 21:07 UTC
    I was thinking, local $.; has the odd, but documented, effect of localizing the last-read-filehandle. Maybe it could be extended to localize the state of ../... too. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://570490]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-25 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found