Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: flip-flop flop

by morgon (Priest)
on Mar 30, 2012 at 11:56 UTC ( [id://962586]=note: print w/replies, xml ) Need Help??


in reply to Re: flip-flop flop
in thread flip-flop flop

So far all approaches seem to avoid the flip-flop.

It would be very curious to see if there is a way to reset the flip-flop in some sane way (you probably need to create a new closure to use in the grep every time you call it or something).

This was one of the very few times I tried to make use of the flip-flop and more or less every time I try to use it I have to abandon it in the end.

Because of this global-state issue the flip-flop is pretty much useless to me - to the point where I think perl would be better off without it...

Replies are listed 'Best First'.
Re^3: flip-flop flop
by BrowserUk (Patriarch) on Mar 30, 2012 at 12:10 UTC
    Because of this global-state issue the flip-flop is pretty much useless to me

    My take is that the flip-flop is very useful for one-liners were is compactness is invaluable, and I wouldn't want to lose that.

    But I cannot remember the last time I used in a script. What it does can always be done better -- though more verbosely -- when you have the space.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      From my point all it would take is a way to reset it's state somehow - then the flip-flop could be handy.

      Is there any reason why this functionality does not (seem to) exist?

        The main reason is that implicit state that must be resetted explicitly is a bad idea. Especially if you compare it to a lexical variable, which provides state explicitly, and is resetted implicitly at the end of the scope.

        For example if an exception is thrown before you do the reset, the next call of your function will return wrong results.

        And what if there are multiple flip-flops in scope? How do you control which one is reset?

Re^3: flip-flop flop
by BillKSmith (Monsignor) on Apr 01, 2012 at 13:47 UTC
    One way to "reset" the flip-flop is to recompile its statement.
    use strict; my @a = qw(hubba bubba abba zappa); print blah("bubba", @a), "\n"; print blah("bubba", @a), "\n"; sub blah { my($look_for, @array)=@_; my @elements_we_want; my $string = '(undef, @elements_we_want) = grep { $_ eq $look_for .. 1 } @a +rray;'; eval $string; return @elements_we_want; }
    Edit: Sorry, I did not notice the similiar idea by JavaFan.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-26 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found