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

Re: [MacPerl] ?:



At 08.10 -0800 1999.12.30, 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).

Two things.  First, you are missing the precedence problem.  Try this to
see the problem:

  $a == 1 ? ($b = 1) : ($b = 0);

However, this is the better way, considering that the ?: operator is meant
for returning a value:

  $b = $a == 1 ? 1 : 0;

-- 
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])


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