[Israel.pm] Golf

Yosef Meller mellerf at netvision.net.il
Wed Nov 2 12:57:48 PST 2005


How's that:
my ($Done) = grep {defined} what(0), "etwas";


Madani, Srikanth, VF-DE wrote:
> Hi,
> 
> I have the following code:
> 
> #!/usr/local/bin/perl -w
> use strict;
> sub what($);
> 
> my $Done;
> $Done = what(0);
> if (!defined($Done)) {
> 	$Done = "etwas";
> }
> 
> sub what($) {
>         return 1 if ($_[0]);
>         return undef;
> }
> 
> I wonder if it's possible to reduce the five lines of code in the middle
> to a single (elegant) line?
> 
> I tried:
> 
> my $Done = defined(what(0)) ? what(0) : "etwas";
> # works fine but involves an extra (unnecessary and possible harmful)
> call to what()
> 
> 
> The following doesn't compile:
> my $Done = what(0) || $Done = "etwas";
> 
> Neither does this:
> $Done = what(0) || my $Done = "etwas";
> 
> 
> Of course, I can go to two lines with the straightforward:
> 
> my $Done = what(0);
> $Done = "etwas" if (!defined($Done));
> # Is this the most concise form?
> 
> Cheers,
> Srikanth Madani
> 
> bash#> PATH=violence! /usr/bin/which sense
> 
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
> 


-- 
  "It doesn't just rock - it plays German Heavy Metal!"
- from some forum I can't remmember now (probably spreadfirefox.com)



More information about the Perl mailing list