[Israel.pm] Golf

Mikhael Goikhman migo at homemail.com
Wed Nov 2 13:47:33 PST 2005


On 02 Nov 2005 18:35:45 +0100, Madani, Srikanth, VF-DE wrote:
> 
> my $Done = defined(what(0)) ? what(0) : "etwas";
> # works fine but involves an extra (unnecessary and possible harmful)
> call to what()

Yes, this is quite a frequent construction. Better do it in 2 lines:

  my $done = what(0);
  $done = "etwas" unless defined $done;

Perl 6 (and 5.10, I think) will have a shortcut for this:

  my $done = what(0) // "etwas";

that should work like "||" or "or", but it preserves 0 and "" values.

  my $answer = ask("yourself") // ask("friend") // ask("mother");
  $answer //= ask("mailing list") // 42;

Regards,
Mikhael.

-- 
perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'



More information about the Perl mailing list