[Israel.pm] Re: Golf
Yosef Meller
mellerf at netvision.net.il
Fri Nov 4 06:43:57 PST 2005
Roie Marianer wrote:
>>$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:
Wrong. My grep solution explicitly checks if the value is defined.
This was my solution, if you missed it:
my ($Done) = grep {defined} what(0), "etwas"
> 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
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
>
More information about the Perl
mailing list