Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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.

Comment on Perl ternary operator style
Select or Download Code
Re: Perl ternary operator style
by BrowserUk (Pope) 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 (Monsignor) 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 SimonClinch (Chaplain) on May 30, 2011 at 09:16 UTC
    Note: The OP asked about having two invocations of a function, not one.

    But of course, the mere fact respondents misread it, suggests it needs to be made more readable. I would reserve the ternary operator for simpler situations rather than for chaining functions (logically for three functions you need two such operators on the same line - gets even more hairy!). There is also an omission - if the second function call returns false, it doesn't get the default value because it is performed on the wrong branch of the ternary for that.

    I think the simplest way, probably most compatible with your style is:

    if ( $target = Function1($params1) || $default ) { $target = Function2( $params2 ) || $default; }

    One world, one people

      Well, before calling other people misreader you better double check that it's not you who has actually misread.


      You can lead your users to water, but alas, you cannot drown them.
        So what in my post suggested that I didn't double check?

        Anyway, you were warned about it and even then, by inference, managed to misread the beginning of the OP! Who is it that has to double check? Try counting the number of function calls in the first code block of the OP.

        One world, one people

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.

Log In?

What's my password?
Create A New User
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 perusing the Monastery: (7)
As of 2014-07-30 04:11 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (229 votes), past polls