Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Why does the Perl conditional operator not do what I expect?

by vinoth.ree (Monsignor)
on Apr 20, 2009 at 10:43 UTC ( #758663=perlquestion: print w/replies, xml ) Need Help??

vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Why does the Perl conditional operator not do what I expect?
by linuxer (Curate) on Apr 20, 2009 at 10:50 UTC
Re: Why does the Perl conditional operator not do what I expect?
by betterworld (Curate) on Apr 20, 2009 at 10:50 UTC

    Running this with perl -MO=Deparse,-p gives:

    (($condition ? ($a = 2) : $a) = 3);

    So your problem is operator precedence. You should parenthize the assignments. (The ternary operator is often found on the right-hand side of an assignment, so it has higher precedence.)

Re: Why does the Perl conditional operator not do what I expect?
by why_bird (Pilgrim) on Apr 20, 2009 at 10:59 UTC
    I am always tripping up over this, and like betterworld says, parentheses are the answer. I believe this also works, and may be slightly easier to get your head around/remember (it is for me anyway).
    ($condition) ? ($a=2) : ($a=3);

    btw from previous experience, I found it quite difficult to super search or google for advice on this construct, as most search engines do not recognise most punctuation, and I for one did not know it was called 'ternary ?'.

    update: p.s. in response to linuxer, 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?

    cheers
    why_bird
    ........
    Those are my principles. If you don't like them I have others.
    -- Groucho Marx
    .......
      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
      Hmmm ,

      ...I found it quite difficult to super search or google... - not if you search for it by name i.e. search terms: tertiary operator perl, - approx. 24,500 hits.

      A user level that continues to overstate my experience :-))
        No, I appreciate that, but I didn't know what it was called :) Since discovering 'perlop' my life has become much easier!
        ........
        Those are my principles. If you don't like them I have others.
        -- Groucho Marx
        .......
Re: Why does the Perl conditional operator not do what I expect?
by Corion (Pope) on Apr 20, 2009 at 13:20 UTC
Re: Why does the Perl conditional operator not do what I expect?
by ww (Archbishop) on Apr 20, 2009 at 10:51 UTC
    This is pretty clearly a case where a decent beginner book or Super Search] would answer your question.
Re: Why does the Perl conditional operator not do what I expect?
by codeacrobat (Chaplain) on Apr 20, 2009 at 20:36 UTC
    The ternary operator has a low precedence. Once you have fallen into its trap 2-3 times, you begin start programming more defensivly. With the ternary operator I learned a general rule of programming "Don't try to be smart".
    Something I did wrong quite a lot times:
    @stuff = $arry_or_list ? @what : $foo, $bar;
    P.S. Anyone remembers the epic Mr. Ternary is greater than Mrs. If Else

    print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2021-12-01 19:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (14 votes). Check out past polls.

    Notices?