develooper Front page | perl.beginners | Postings from April 2012

Re: Using the ternary operator to concat a string not working likeI think?

Thread Previous | Thread Next
From:
Steve Bertrand
Date:
April 3, 2012 21:13
Subject:
Re: Using the ternary operator to concat a string not working likeI think?
Message ID:
4F7BCA57.6050205@gmail.com
On 2012-04-03 18:55, timothy adigun wrote:
> Hi Stan,
> Please check my comments below:

>      $test{one} eq "first" ?
>      $test{one} .= " is the worst\n" :
>      ( $test{two} .= " is the best\n");

This is not what the ternary (conditional operator) is for. As I said in 
my last post, it is used for selecting from two values, NOT for making 
changes because of a condition.

>   You can also see perldoc perlop, under *Conditional Operator* sub-topic.

Where it states, as its opening statement:

"Ternary "?:" is the conditional operator, just as in C. It works much 
like an if-then-else. If the argument before the ? is true, the argument 
before the : is returned, otherwise the argument after the : is returned."

perlop doesn't even hint that it is used to modify existing variables. 
Even the examples are clear cut and dried.

Moreover, there isn't a single example in that perlop section that 
describes using parenthesis in the fashion your advice above shows. The 
Perlish way to use parens with the conditional operator was described in 
my last email. Here it is here again:

$x = ( $y == 1 )
   : 5
   : 10;

Here's the link to the perlop conditional op section again:

http://perldoc.perl.org/perlop.html#Conditional-Operator

Cheers,

Steve

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About