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

Re: [MacPerl-Forum] Conditional operator problem



On Mon, Jun 12, 2000 at 12:02:00PM +0100, Campbell-Lange wrote:
> I am having some difficulty using the ?: notation on MacPerl 5.2.0r4.
> 
> Option 1 is working, while option 2 always results in 'Ý'. If I remove the
> scalar assignment within the ? to print, it works fine. Should I not try to
> reassign $reply using ?: notation? 
> 
> set $reply :    $reply = $subject =~ /Re:/;
> option 1   : if ($reply eq 1) { $reply = '+' } else {$reply = 'Ý'};
> option 2   :    ($reply eq 1) ? $reply = '+' : $reply = 'Ý';
> 
> I note that the pod says 'The operator may be assigned to if both the 2nd and
> 3rd arguments are legal lvalues'.

The POD also says:

       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)


So you needed to add parentheses to get the proper precedence.

But if you're assigning to the same variable in both conditions, you might
want to do it like this instead:

$reply = $reply eq 1 ? '+' : '?';

Ronald

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