Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Perl ternary operator style

by SimonClinch (Deacon)
on May 30, 2011 at 09:16 UTC ( #907288=note: print w/replies, xml ) Need Help??

in reply to Perl ternary operator style

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

Replies are listed 'Best First'.
Re^2: Perl ternary operator style
by holli (Monsignor) on May 30, 2011 at 09:30 UTC
    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

        ry counting the number of function calls in the first code block of the OP.

        You're right there are two function calls. But they are both called Function(), not Function1() and Function2() as in your example.

        Whirrr-click-whirrr. Yes. That's right. The OP is calling the same function twice!.

        And if you looked just a few lines further down, he acknowledges in his second code sample where he tries to avoid that:

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

        It's one thing to misread a post, we've all done that. But to fail to double check when it is suggested you should; and then claim you already did; and then respond with such .... (I'll let you fill in the rest.)

        Nice one. I bet you're a Jackass fan aren't you.

        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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://907288]
[LanX]: Do students have the constitutional right to get armed to defend themself against armed teachers?
[Discipulus]: vasanth.easyrider perseverare diabolicum

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2018-02-23 12:28 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (302 votes). Check out past polls.