On Friday 11 January 2008 16:16:27 Zembower, Kevin wrote:
> When I execute this line:
>
> $type eq "unknown" ? $type="human" : $type="both";
>
> $type is always "both". But executing this line:
>
> if ($type eq "unknown") {$type="human"} else {$type="both"};
>
> $type is "human", which is want I want and expect. The context for these
> statements in my program is pasted in at the bottom.
>
> Thanks for your guidance.
>
> -Kevin

The ?: operator is called the "ternary" operator. The result of the following 
line differs from what you expect because of the relative precedences of the 
ternary operator and the assignment operator.

$type eq "unknown" ? $type="human" : $type="both";

From `perdoc perlop`:

       Because this operator produces an assignable result, using assignments
       without parentheses will get you in trouble.  For example, this:

           $a % 2 ? $a += 10 : $a += 2

       Really means this:

           (($a % 2) ? ($a += 10) : $a) += 2

       Rather than this:

           ($a % 2) ? ($a += 10) : ($a += 2)

Stephen Kratzer
Network Engineer II
CTI Networks, Inc.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to