[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] ?:



On Thu, Dec 30, 1999 at 08:10:03AM -0800, Nicholas G. Thornton wrote:
> Lately (ie since I discovered it) I've been having problems with the 
> ?: operator. That being something like... 
> 
> $a == 1 ? $b = 1 : $b = 0 ; 
> 
> ... will *always* set $b to zero. Irregardless of whether the first 
> part succeds of fails. Is there something I'm missing about the 
> operator? Note I've been coding on ActivePerl (windoze98) not MacPerl 
> (because I'm on the other cost from my mac, and there are only pcs 
> here). 
> 

I hope you won't mind if I throw in a reminder to RTFM.

perlop:

       Conditional Operator

       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.

       [...]

       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)


Ronald

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org