Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

?: (conditional operator)

by pmc2 (Acolyte)
on Aug 23, 2001 at 17:27 UTC ( #107313=perlquestion: print w/replies, xml ) Need Help??

pmc2 has asked for the wisdom of the Perl Monks concerning the following question:

Could someone please explain to me why the following snippet prints "Yo helloBye" and not just "Yo hello"?
perl -e '$x="\nYo "; print ($x ? $x.="hello" : $x.="Bye");'
I thought the above example was equivalent to:
perl -e '$x="\nYo "; if ($x) {print $x."hello";} else {print $x."Bye" +;}'
which correctly prints "Yo hello". Any thoughts?

Replies are listed 'Best First'.
Re: ?: (conditional operator)
by Masem (Monsignor) on Aug 23, 2001 at 17:46 UTC
    Based on perlop , the terany operator (?:) is just one precidence level above the "operate and set" operators .=, +=, etc.

    Thus, perl is seeing:

    print ($x ? $x.="hello" : $x.="Bye");
    as
    print ( ( $x ? $x.="hello" : $x ) .="Bye");
    and not as
    print ( $x ? $x.="hello" : ( $x.="Bye" ) );
    The solution is, therefore, to use the last form to get it the way you want.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    It's not what you know, but knowing how to find it if you don't know that's important

      Ovid just asked similar Weird syntax question and htoug has elegant solution using wonderfull B::Deparse module.

      pmas
      To make errors is human. But to make million errors per second, you need a computer.

Re: ?: (conditional operator)
by rchiav (Deacon) on Aug 23, 2001 at 17:38 UTC
    Actually, you can modify $x, but you need to quantify what you want to be executed contitionaly.. This will work..
    perl -e '$x="\nYo "; print $x ? ($x.="hello") : ($x.="Bye");'
    Hope this helps
    Rich
      Hence, the problem seems to be in the expectation of .= and not print.

      ------
      /me wants to be the brightest bulb in the chandelier!

      Vote paco for President!

Re: ?: (conditional operator)
by tune (Curate) on Aug 23, 2001 at 17:37 UTC
    perl -e '$x="\nYo "; print ($x ? $x."hello" : $x."Bye");'

    This one is equal.

    --
    tune

Re: ?: (conditional operator)
by busunsl (Vicar) on Aug 23, 2001 at 17:32 UTC
    You can't modify $x in the print statement and expect the result you are expecting.

    This will work:

    perl -e '$x="\nYo "; $x ? $x.="hello" : $x.="Bye";print $x;'
Re: ?: (conditional operator)
by Cine (Friar) on Aug 23, 2001 at 17:49 UTC
    This looks like a bug in perl. It seems that both the true and false part is evalueted wrongly, if there is '=' in the false case.
    I've tried the following:
    perl -e '$x="\nYo "; print ($x ? $x.="hello" : $x.="Bye");' => 'Yo hel +loBye' perl -e '$x="\nYo "; print ($x ?($x.="hello") : ($x.="Bye"));' => 'Yo +hello' perl -e '$x="\nYo "; print ($x ? $x."hello" : $x."Bye");' => 'Yo hello +' perl -e '$x="\nYo "; print ($x ? $x."hello" : $x.="Bye");' => Can't mo +dify concatenation (.) or string in concatenation (.) or string at -e + line 1, near ""Bye")" Execution of -e aborted due to compilation errors. perl -e '$x="\nYo "; print ($x ? $x."hello" : ($x.="Bye"));' => 'Yo he +llo'


    Update:

    Nope, not a bug, perl just doenst do what you expect ;( It's a precedence fault as masem points out.

    T I M T O W T D I
      doesn't do what you expect

      Remember the Camel's words: Perl does what you expect, provided you expect the right thing.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2022-06-27 23:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (89 votes). Check out past polls.

    Notices?