in reply to Re: Re: Implication operator
in thread Implication operator

Update: As chipmunk indicates, ^ is more akin to | than || (bit-wise versus otherwise) and it does not make sense to use it as a comparison operator. So while xor can still be used, given its low precedence, akin to 'or' in comparisons, it does not have a '^^' counterpart akin to ||, which could be potentially very useful. I'm keeping the rest of this post here for historical purposes.

I don't really share your feelings about 'xor' precedence. Two situations where its precedence allows you to forego parenthesis:

if ($a || $b xor $c || $d) { ... } if ($a xor $b) { ... }
In the first example, would you rather xor's precedence be higher than ||? Now look at what we have to do:
if (($a || $b) xor ($c || $d)) { ... }
If we want to do swap the xor/|| operators, I can possibly see the advantage, but really any complex situation like this is going to almost require you to use paranthesis in order to show Perl how you want things evaluated. Perhaps you are confusing 'xor' with the bitwise/integer xor operator (^), which has a much higher precedence. See perlop.
if ($a ^ $b || $c ^ $d) { ... }
Realistically (and I don't know why I didn't mention this before), this should probably be used in expressions instead of 'xor', unless you need something that's low precedence (like using 'or' or 'and' in expressions).

Replies are listed 'Best First'.
Re: Re: Re: Re: Implication operator
by chipmunk (Parson) on Dec 11, 2000 at 21:52 UTC
    Using a bitwise xor operator as a logical xor operator can be dangerous.
    sub true { return 1; } sub True { return 2; } if (true() xor True()) { print "xor is true.\n"; } if (true() ^ True()) { print "^ is true.\n"; } __END__ ^ is true.
    The obvious choice for a high-precedence logical xor is ^^. It's consistent with && and ||, and it doesn't conflict with anything already in the language.
      True enough! For some reason I was thinking along the lines of comparisons, and in my head I placed &&, || and ^ in the same boat, when it really needs to be && and || in one boat and ^, | and & in another. I agree that something like ^^ might be nice.