From 52b49c4323f987dde76b0897e6aadc8524cbf195 Mon Sep 17 00:00:00 2001 From: Martijn Lievaart Date: Wed, 14 Feb 2024 19:30:29 +0100 Subject: [PATCH] Add test and docs for (to be written) ^^ operator --- pod/perlop.pod | 9 ++++++++- t/op/lop.t | 11 +++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/pod/perlop.pod b/pod/perlop.pod index 72c76c2db211b..91ac7b5abf939 100644 --- a/pod/perlop.pod +++ b/pod/perlop.pod @@ -140,7 +140,7 @@ values only, not array values. left & &. left | |. ^ ^. left && - left || // + left || ^^ // nonassoc .. ... right ?: right = += -= *= etc. goto last next redo dump @@ -1019,6 +1019,13 @@ if the left operand is true, the right operand is not even evaluated. Scalar or list context propagates down to the right operand if it is evaluated. +=head2 C-style Logical Xor +X<^^> X + +Binary C<"^^"> performs a logical XOR operation. Both operands are +evaluated and the result is true only if exactly one of the operands is true. +Scalar or list context propagates down to the right operand. + =head2 Logical Defined-Or X X diff --git a/t/op/lop.t b/t/op/lop.t index 2f1ad2054cde5..08d6eb694e4ed 100644 --- a/t/op/lop.t +++ b/t/op/lop.t @@ -1,7 +1,7 @@ #!./perl # -# test the logical operators '&&', '||', '!', 'and', 'or', , 'xor', 'not' +# test the logical operators '&&', '||', '^^', '!', 'and', 'or', , 'xor', 'not' # BEGIN { @@ -10,7 +10,7 @@ BEGIN { set_up_inc('../lib'); } -plan tests => 33; +plan tests => 47; for my $i (undef, 0 .. 2, "", "0 but true") { my $true = 1; @@ -104,4 +104,11 @@ for my $test ( ) { my ($a,$b, $exp) = @$test; is(($a xor $b), $exp, "($a xor $b) == '$exp'"); + is(($a ^^ $b), $exp, "($a ^^ $b) == '$exp'"); } + +# precedence +is((1 xor 1 and 0), 1, '(1 xor 1 and 0) == 1'); +is((1 xor 0 or 1), '', "(1 xor 0 or 1) == ''"); +is((1 ^^ 1 && 0), 1, '(1 xor 1 and 0) == 1'); +is((1 ^^ 0 || 1), '', "(1 ^^ 0 || 1) == ''");