>> Assignment has lower precedence than ?:  It is done last.

Aha -- I was afraid of something like that.

> (I.e: put parentheses around the clauses of if statements, even though they
> are not needed or I could use the ultra-low-precedence "and"/"or"/etc.)

Good idea.  I almost always do in if-statements, but it hadn't
occurred to me that it might be a good idea to do the same thing
*after* the conditional!

Thanks for the help everyone.


Best,
Jeff

On Mon, Mar 29, 2010 at 10:35 AM, Shlomi Fish <shlo...@iglu.org.il> wrote:
> Hi Shawn!
>
> On Monday 29 Mar 2010 17:19:36 Shawn H Corey wrote:
>> On Mon, 29 Mar 2010 10:02:15 -0400
>>
>> Jeff Soules <sou...@gmail.com> wrote:
>> >     ($foo == 1) ?
>> >
>> >     $bar = 0 :
>> >     $bar = 1;
>> >
>> > Am I doing something stupid or missing something obvious?
>>
>>     ($foo == 1) ?
>>       ( $bar = 0 ) :
>>       ( $bar = 1 );
>>
>> Assignment has lower precedence than ?:  It is done last.  What you
>> wrote above is:
>>
>>     (($foo == 1) ?
>>       $bar = 0 :
>>       $bar ) = 1;
>>
>> Which assigns 1 to $bar regardless of what $foo is.
>
> Thanks for the clarification. I suspected something of this sort was the case.
> Whenever I don't remember what the operator precedence is, I always add a few
> parentheses. I still often tend to do something like:
>
> {{{
> if (($age > 18) && ($name eq "Catherine"))
> }}}
>
> (I.e: put parentheses around the clauses of if statements, even though they
> are not needed or I could use the ultra-low-precedence "and"/"or"/etc.)
>
> Regards,
>
>        Shlomi Fish

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to