>>>>> "PH" == Patrick Hartman <> writes:
PH> Hi, I am trying to use ? : statements instead of if/else blocks
PH> wherever possible to keep my code shorter. Below is a conditional
PH> statement that for some reason is not working as expected with the ? :
PH> method:
this is a classic newbie misuse of ?:.
PH> my $style;
PH> $brand eq 'nb' ? $style = $bare_style . $color : $style =
PH> $bare_style;
PH> print "?: version: $style\n";
$style = $brand eq 'nb' ? "$bare_style$color" : $bare_style ;
?: is meant to return an expression based on a boolean. it it NOT meant
to do side effects like assignment. you could fix your code with () but
it would still be wrong. the issue was precedence. the proper solution i
pasted above. the whole idea is to assign once but choose what to
assign. then the precedence (= is higher binding than ?

works to your
advantage.
and given what you are doing, there are other ways too:
$style = $bare_style . ($brand eq 'nb' ? $color : '') ;
$style = $bare_style ;
$style .= $color if $brand eq 'nb' ;
uri
--
Uri Guttman ------
--------
http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ----
http://bestfriendscocoa.com ---------