in reply to Safe to use ternary operator like this?

I have to run, but I just saw this...
this is how I would do it:
my $Temp=&Func() || 'undefined';

Bye!!

Replies are listed 'Best First'.
Re: Re: Safe to use ternary operator like this?
by arturo (Vicar) on Nov 16, 2000 at 20:48 UTC

    This will set $Temp to 'undefined' if &Func returns a false value (see What is true and false in Perl? for more details). I like the original code, methinks. It's just what the ternary operator was designed to do.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Re: Safe to use ternary operator like this?
by Albannach (Monsignor) on Nov 16, 2000 at 20:43 UTC
    Not quite the same thing depending on the sub return though:
    use strict; my $temp; $temp = defined ($temp = func()) ? $temp : 'undefined'; print "func:$temp\n"; $temp = func() || 'undefined'; print "func:$temp\n"; sub func { return ''; }
    produces:
    func: func:undefined