[Israel.pm] Golf

Madani, Srikanth, VF-DE Srikanth.Madani at vodafone.com
Wed Nov 2 09:35:45 PST 2005


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




More information about the Perl mailing list