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
In Section
Seekers of Perl Wisdom