Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
P is for Practical
 
PerlMonks  

•Re: improve ugly flow control

by merlyn (Sage)
on Sep 19, 2004 at 03:53 UTC ( [id://392137]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to improve ugly flow control

Just a different try on this:
use List::Util qw(first); if (defined(my $try = first { $hash{$_} } @options)) { do_something($try); } else { log_failure(); }

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re^2: improve ugly flow control
by perrin (Chancellor) on Sep 19, 2004 at 16:52 UTC
    That's a neat idea. Unfortunately, in my real code, the $hash{$_} test is actually something expensive, so I need to stop as soon as one of the options works. I should have made a better example.
Re^2: improve ugly flow control
by Aristotle (Chancellor) on Sep 20, 2004 at 05:50 UTC

    I liked that quite a bit, but the structure has been bothering me, like with most other solutions: it feels to me like the failure case gets too much emphasis. I didn't know how to do any better when I first saw the post, though. But now I think I do:

    { my $try = first { $hash{ $_ } } @options; defined $try or ( log_failure(), last ); do_something( $try ); }

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://392137]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.