Perl Gotcha : xor precedence

Perl has a logical xor operator but it happens to be at the very bottom of the precedence priority list , importantly below the humble comma. So this innocent looking piece of code:

foobar( $a xor $b, $c );

Is interpreted as:

foobar( $a xor ($b, $c) );

And note unlike the not, and and or low-precedence logical operators,  there is no high-precedence xor operator.

So it’s a good idea to wrap xors in parentheses to guarantee intended behaviour:

foobar( ($a xor $b), $c );

Leave a Reply

Your email address will not be published. Required fields are marked *