ternary conditional operator 
Author Message
 ternary conditional operator

Why does this print no? I can't figure it out:

___________________________
#!/usr/bin/perl

$director = "else";

$director =~ /else/ ? $redirto = "yes" : $redirto = "no";

print $redirto;

____________________________



Wed, 16 Jul 2003 04:57:43 GMT  
 ternary conditional operator

  JH> Why does this print no? I can't figure it out:

  JH> ___________________________
  JH> #!/usr/bin/perl

  JH> $director = "else";

  JH> $director =~ /else/ ? $redirto = "yes" : $redirto = "no";

your precedence is wrong. ?: binds tighter than =

regardless, you should do it this way:

$redirto = $director =~ /else/ ? "yes" : "no" ;

uri

--

SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page  -----------  http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net  ----------  http://www.northernlight.com



Wed, 16 Jul 2003 05:07:25 GMT  
 ternary conditional operator
"Uri Guttman" wrote

Quote:
> >>>>> "JH" == John Hall writes:

>   JH> Why does this print no? I can't figure it out:
>   JH> $director = "else";
>   JH> $director =~ /else/ ? $redirto = "yes" : $redirto = "no";

> your precedence is wrong. ?: binds tighter than =

I couldn't figure it out.  I [ use O 'Deparse' ] to get this deparsing

$director =~ /else/ ? ($redirto = "yes") : $redirto = "no";

It looks good.  Just like a normal [ =~ ? ( = ) : ( = ) ] .
Could you further explain what's wrong here?

Thank you.

John Lin



Fri, 18 Jul 2003 09:09:12 GMT  
 ternary conditional operator

Quote:

> "Uri Guttman" wrote
>> >>>>> "JH" == John Hall writes:

>>   JH> Why does this print no? I can't figure it out:
>>   JH> $director = "else";
>>   JH> $director =~ /else/ ? $redirto = "yes" : $redirto = "no";

>> your precedence is wrong. ?: binds tighter than =
> I couldn't figure it out.  I [ use O 'Deparse' ] to get this deparsing
> $director =~ /else/ ? ($redirto = "yes") : $redirto = "no";
> It looks good.  Just like a normal [ =~ ? ( = ) : ( = ) ] .
> Could you further explain what's wrong here?

Your "normal" example has two sets of parens.  The deparsed output has
only one set.  What Uri was saying is that the *entire* ?: operator
(which, as you remember, has three operands) binds tighter than =.  The
$redirto="yes" will be evaluated properly because it's the second operand
of the ternary conditional, but the $redirto="no" won't be evaluated as
such because the $redirto gets parsed as the third operand of the
conditional, and the entire conditional is taken as the left-hand sign of
the final =.


Fri, 18 Jul 2003 09:27:09 GMT  
 ternary conditional operator

 | your precedence is wrong. ?: binds tighter than =
 |
 | regardless, you should do it this way:
 |
 | $redirto = $director =~ /else/ ? "yes" : "no" ;
 |
 | uri

heh I'm glad I'm reading today -- I'd completely forgotten this little
factoid. :)

--
send mail to mactech (at) webdragon (dot) net instead of the above address.
this is to prevent spamming. e-mail reply-to's have been altered
to prevent scan software from extracting my address for the purpose
of spamming me, which I hate with a passion bordering on obsession.  



Fri, 18 Jul 2003 10:59:40 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Ternary Conditional Operator Question

2. problem with ternary operator

3. ternary operator

4. A question on the Conditional Operator

5. Conditional Operator behavior?

6. Three Operand Conditional Operator

7. push onto trinary conditional operator?

8. newbie questions re conditional operators

9. Q: conditional operator and variable assignment

10. conditional operator "?:"

11. problem parsing conditional operator.

12. mixing defined, assignments, and ternaries

 

 
Powered by phpBB® Forum Software