perl5 icon indicating copy to clipboard operation
perl5 copied to clipboard

New logical xor operator, spelled `^^`

Open leonerd opened this issue 1 year ago • 1 comments

It was noted on the mailing list 1 that we don't have a (high-precedence) logical xor operator - while we have all three of or, and, xor, we only have ||, &&. This PR adds the missing ^^ operator.

This was previously a syntax error, so there should be no need for a feature flag or similar:

$ perl -MO=Concise -ce '$x ^^ $y'
syntax error at -e line 1, near "^^"
-e had compilation errors.

$ perl -MO=Concise -ce '$x^^$y'
syntax error at -e line 1, near "^^"
-e had compilation errors.

This PR currently still in draft state, because there are two failing tests:

# Failed test 45 - (1 xor 0 or 1) == '' at op/lop.t line 112
#      got "1"
# expected ""
# Failed test 47 - (1 ^^ 0 || 1) == '' at op/lop.t line 114
#      got "1"
# expected ""

However, I feel for both of these the tests are actually incorrect - I will discuss with test author.

leonerd avatar Feb 15 '24 21:02 leonerd

Tests are now found to be fine. This is ready for revew and hopefully merge.

leonerd avatar Feb 18 '24 21:02 leonerd

Should Deparse be updated to be more consistent with how and/or/&&/|| are handled?

$ p -MO=Deparse -e'$x = $y && $z'
$x = $y && $z;
$ p -MO=Deparse -e'$x = ($y && $z)'
$x = $y && $z;
$ p -MO=Deparse -e'$x = $y and $z'
$z if $x = $y;
$ p -MO=Deparse -e'$x = ($y and $z)'
$x = $y && $z;

while

$ p -MO=Deparse -e'$x = $y ^^ $z'
$x = ($y xor $z);
$ p -MO=Deparse -e'$x = ($y ^^ $z)'
$x = ($y xor $z);
$ p -MO=Deparse -e'$x = $y xor $z'
$x = $y xor $z;
$ p -MO=Deparse -e'$x = ($y xor $z)'
$x = ($y xor $z);

So Deparse currently always uses the high-precedence &&/|| (plus 'if'), while it always always uses the low-precedence xor.

Actually, thinking about it, maybe we should use some op_private flags on all three ops to distinguish &&/and/if etc for better use by Deparse? But that would be post 5.40.

-- If life gives you lemons, you'll probably develop a citric acid allergy.

iabyn avatar Feb 29 '24 10:02 iabyn