[Israel.pm] Re: Golf

Roie Marianer roiem at actcom.co.il
Fri Nov 4 02:14:10 PST 2005


> $Done = what(0) || "etwas";
All the solutions that were presented here, except for the Perl 6 ones 
(Mikhael Goikhman and Gaal Yahas) have a subtle flaw that can result if your 
"what" function returns false values:

sub what($) {
 return 2 if ($_[0] eq "two");
 return 0 if ($_[0] eq "zero");
 return undef;
}

my $done=what("two") || "undefined"; # 2, OK
$done=what("something") || "undefined"; # "undefined", OK
$done=what("zero") || "undefined"; # "undefined", but you wanted 0

The // operator in Perl 6 will do exactly what you wanted, but until then, be 
careful. (Your "what" function never returned false values, so it worked for 
you).

-R



More information about the Perl mailing list