Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Perl ternary operator style

by David Clarke (Initiate)
on May 29, 2011 at 16:38 UTC ( #907228=perlquestion: print w/replies, xml ) Need Help??

David Clarke has asked for the wisdom of the Perl Monks concerning the following question:

Is there a Monk who'll help my perling-style? In my scripts I often use ternary operators of the kind:

$Target = Function($Params) ? Function($params) : $DefaultValue;

This example is simple enough. But if Function() takes multiple parameters, and they are themselves complex (e.g. using dereferencing or array / hash indexing), there is a risk that the two invocations may be subtly mis-typed; not to mention side-effects from calling the function multiple times. My question is - Is there a way to simplify this construct? Is there something I can use between the '?' and ':' that gets the result from imediately left of the '?' ? On possible solution using an intermediate result:

$Target = ($temp = Function($params)) ? $temp : $DefaultValue;

breaks up the elegant left-to-right sequence of the code. And an elegant sequence help understanding and maintainability.

Replies are listed 'Best First'.
Re: Perl ternary operator style
by BrowserUk (Patriarch) on May 29, 2011 at 16:56 UTC

    I think what you are asking for is:

    $Target = Function($Params) // $DefaultValue;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Perl ternary operator style
by salva (Canon) on May 29, 2011 at 16:56 UTC
    $Target = Function($params) || $DefaultValue;
    Or in not so ancient perls, usually...
    $Target = Function($params) // $DefaultValue; better.
Re: Perl ternary operator style
by JavaFan (Canon) on May 29, 2011 at 17:12 UTC
    If you want X if X is true, otherwise Y, that operator is usually written ||. The only reason to use X ? X : Y instead is when you want to evaluate X twice. But that's not what you want according to your description. So:
    $Target = Function($params) || $DefaultValue;
Re: Perl ternary operator style
by TomDLux (Vicar) on May 30, 2011 at 15:35 UTC

    I agree with BrowserUK and others about appropriate use of || or //.

    More generally, I would say ternary operator is great when you have simple values to compare. If the terms become complicated, you want something else, maybe an actual IF block. It's not just a matter of making the code work correctly. The code needs to be decipherable for the sucker who has to read it six months from now to implement some change requirement.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

      Many thanks, TomDLux (and others) for pointing out that my use of ?: in this kind of situation is blindingly (to the subsequence reader) over-complex. I think this is a case where copy-paste-reuse has become embedded in some of my work, and ternary is the norm. I'll go an re-evaluate what is actually needed. David Clarke.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://907228]
Approved by salva
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2022-05-29 00:24 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (101 votes). Check out past polls.