>>>>> "SHC" == Shawn H Corey <[email protected]> writes:
SHC> Assignment has lower precedence than ?: It is done last. What you
SHC> wrote above is:
SHC> (($foo == 1) ?
SHC> $bar = 0 :
SHC> $bar ) = 1;
SHC> Which assigns 1 to $bar regardless of what $foo is.
when will people learn?? the ternary operator is for CHOOSING A
VALUE. it is NOT for side effects. its precedence is designed to allow
it to be assigned, NOT to have assignments done inside.
NEVER USE ?: for side effects. ALWAYS use it to return a value chosen
from two. do not break this rule and it will always work.
$bar = ($foo == 1) ? 0 : 1 ;
that is the proper answer to the OP's question. no side effects, only
ONE use of $bar and easy to read. the () are optional but i like them to
highlight what the boolean part is.
uri
--
Uri Guttman ------ [email protected] -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/