in reply to ?: (conditional operator)

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

Replies are listed 'Best First'.
Re: Re: ?: (conditional operator)
by echo (Pilgrim) on Aug 23, 2001 at 18:08 UTC
    doesn't do what you expect

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