Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Warning while using ternary operator

by AnomalousMonk (Abbot)
on Dec 16, 2013 at 23:15 UTC ( #1067386=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Warning while using ternary operator
in thread Warning while using ternary operator

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 :)


Comment on Re^3: Warning while using ternary operator
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1067386]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2015-07-04 02:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls