Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Why does the Perl conditional operator not do what I expect?

by AnomalousMonk (Bishop)
on Apr 20, 2009 at 13:01 UTC ( #758701=note: print w/replies, xml ) Need Help??


in reply to Re: Why does the Perl conditional operator not do what I expect?
in thread Why does the Perl conditional operator not do what I expect?

say you wanted to do
   $condition ? $a=2 : $b=3
would
   ($condition) ? ($a=2) : ($b=3)
then be the correct/advisible way to do it?
It would be one way to do it with a ternary operator. Another way is shown below. Both are correct (i.e., compile without error). Which is 'advisable' depends on circumstances; I tend to prefer the ternary form below, but
   if ($cond) { $a = 2 } else { $b = 3 }
seems the clearest and most advisable of all.
>perl -wMstrict -le "my $cond = shift; $a = $b = 99; ($cond ? $a : $b) = ($cond ? 2 : 3); print qq{a $a b $b}; " 0 a 99 b 3 >perl -wMstrict -le "my $cond = shift; $a = $b = 99; ($cond ? $a : $b) = ($cond ? 2 : 3); print qq{a $a b $b}; " 1 a 2 b 99

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2021-12-08 22:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (36 votes). Check out past polls.

    Notices?