in reply to Re^5: Short form (ternary) if elsein thread Short form (ternary) if else
$ perl -Mstrict -Mwarnings -E 'my $x; 1 ? $x=1 : $x=0; say $x;'
1 ? $x=1 : $x=0
(1 ? $x=1 : $x)=0
which is then the same as
which is why $x becomes 0.
You can use assignments within ? :, you just need to parenthesize them
1 ? $x=1 : ($x=0)
Adding parentheses to the second statement to show precedence, we get:
( 1 ? ( $x=1 ) : $x ) = 0;
As the 2nd and 3rd arguments are lvalues, you can assign to the ternary operator. As the first argument (1) is TRUE, the assignment becomes:
( $x=1 ) = 0;
Which effectively boils down to:
$ perl -Mstrict -Mwarnings -E 'my $x; ($x = 1) = 0; say $x;'
The link I gave above (perlop manpage - under Conditional Operator) has a fuller description.
An Englishman in New York
An American Werewolf in London
An American in Paris
The Innocents Abroad
An Idiot Abroad
Robinson Crusoe on Mars
A Connecticut_Yankee in King Arthur's Court
There and Back Again
Travels into Several Remote Nations of the World
Results (94 votes). Check out past polls.