Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Sequential defined-or operators

by Util (Priest)
on Jan 14, 2008 at 22:21 UTC ( #662388=note: print w/ replies, xml ) Need Help??


in reply to Sequential defined-or operators

While you remain pre-5.10, you might like this technique:

use List::Util qw( first ); my $theme = first { defined $_ } ( $global_config->{modes}{$current_mode}{theme}, $theme_config->{current_theme}, $default_theme, 'none', );


Comment on Re: Sequential defined-or operators
Download Code
Replies are listed 'Best First'.
Re^2: Sequential defined-or operators
by plobsing (Friar) on Jan 15, 2008 at 02:39 UTC
    Thats all fine and good until your default value is ask_user(). Then this fails due to eager evaluation. // fixes this fortunately.
      It took me a second to understand that you are referring to the short-circuiting nature of //. If you needed the short-circuiting behaviour pre-5.10, here's one solution:
      sub first(&@) { my $cb = shift(@_); for (@_) { my $i = ref eq 'CODE' ? $_->() : $$_; my $rv; $rv = $cb->() for $i; return $i if $rv; } return; } my $theme = first { defined } \$global_config->{modes}{$current_mode}{theme}, \$theme_config->{current_theme}, \&ask_user, \'none';

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (15)
As of 2015-07-31 14:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (278 votes), past polls