Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Warning while using ternary operator

by Anonymous Monk
on Dec 16, 2013 at 13:42 UTC ( #1067329=note: print w/replies, xml ) Need Help??


in reply to Warning while using ternary operator

The ternary operator is not a shorthand way of writing an if block. It's a shorthand way of writing an if block that returns a value.
if ($expression) { $var = "foo"; } else { $var = "bar"; } # WRONG $expression ? $var = "foo" : $var = "bar"; # correct $var = $expression ? "foo" : "bar";

Replies are listed 'Best First'.
Re^2: Warning while using ternary operator
by kcott (Canon) on Dec 16, 2013 at 15:19 UTC

    This is bad information.

    The expression

    $expression ? $var = "foo" : $var = "bar";

    is only "# WRONG" because of operator precedence (see perlop: Operator Precedence and Associativity). Here's how it is parsed:

    $ perl -MO=Deparse,-p -e '$expression ? $var = "foo" : $var = "bar";' (($expression ? ($var = 'foo') : $var) = 'bar'); -e syntax OK

    This is explained in perlop: Conditional Operator. Correcting the precedence with parentheses:

    $ perl -MO=Deparse,-p -e '$expression ? ($var = "foo") : ($var = "bar" +);' ($expression ? ($var = 'foo') : ($var = 'bar')); -e syntax OK

    I do not dispute that '$var = $expression ? "foo" : "bar";' is a better way to write '$expression ? ($var = "foo") : ($var = "bar");'. In fact, the documentation also recommends this.

    Once corrected as indicated, here's how your "# WRONG" code behaves:

    #!/usr/bin/env perl -l use strict; use warnings; for my $expression (0, 1) { my $var = ''; $expression ? ($var = 'foo') : ($var = 'bar'); print "\$expression = '$expression'; \$var = '$var'"; }

    Output:

    $expression = '0'; $var = 'bar' $expression = '1'; $var = 'foo'

    -- Ken

      But what does the syntactically correct statement
          $expression ? $var = "foo" : $var = "bar";
      actually do?

      >perl -wMstrict -le "for my $expression (0, 1) { my $var = ''; $expression ? $var = 'foo' : $var = 'bar'; print qq{if \$expression is $expression, \$var is '$var'} } " if $expression is 0, $var is 'bar' if $expression is 1, $var is 'bar'

      It seems the statement is just an expensive equivalent to the statement
          $var = 'bar';
      The reason why is left as a exercise for simonz (after the recommended perusal of perlop :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1067329]
help
Chatterbox?
[marto]: good morning all
[Corion]: Hi marto!
[Corion]: The fun show at $work continues, as The Big Project is now in its second week of frantic live-bugfixing and weekend releases where nobody knows what went live. Nothing has been tested anyway.
erix mutters cantankerously under his breath
Corion watches from the sidelines. Or rather, from behind, as my system only gets output from that process and my programs adhere strictly to the GIGO design principle.
[erix]: ah, that's nice to hear Corion :)
[Corion]: erix: Yeah, the sad thing is that all I can do is document things, so I can point fingers when the auditors come :-/
[Corion]: "I'm here to open tickets and point fingers. And I'm all out of tickets."
[erix]: didn't Sybase have pretty good auditing? :) (this is a vague memory)

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2017-03-28 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (328 votes). Check out past polls.