in reply to Assignment to a value only if it is defined
If bar() is a complex function, then perhaps it should be the one returning the default value...
If that's not reasonable, then perhaps you need a default wrapper or something... Here's two ideas:
sub mybar { my $t = bar(); return "default" unless defined $t; return $t; } sub default { my $to_call = shift; my $default = shift; my $t = to_call->(@_); return $default unless defined $t; return $t; }
Or just do what the others said above (5.10+ only): $x = bar() // "default";
-Paul
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Assignment to a value only if it is defined
by voj (Acolyte) on Jan 20, 2010 at 15:44 UTC | |
by jdporter (Paladin) on Jan 20, 2010 at 16:40 UTC | |
by ikegami (Patriarch) on Jan 20, 2010 at 19:02 UTC |
In Section
Seekers of Perl Wisdom