Skip to content

Commit 99824bd

Browse files
committed
perlop: Add detail about xor
Fixes Perl#18565
1 parent 9f953fc commit 99824bd

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

pod/perlop.pod

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -987,9 +987,13 @@ C<"experimental::bitwise"> category.
987987
X<operator, bitwise, or> X<bitwise or> X<|> X<operator, bitwise, xor>
988988
X<bitwise xor> X<^>
989989

990-
Binary C<"|"> returns its operands ORed together bit by bit.
990+
Binary C<"|"> returns its operands ORed together bit by bit. If both
991+
corresponding bits are 0, the resulting bit is 0; if either is 1, the result is
992+
1.
991993

992-
Binary C<"^"> returns its operands XORed together bit by bit.
994+
Binary C<"^"> returns its operands XORed together bit by bit. If both
995+
corresponding bits are 0 or both are 1, the resulting bit is 0; if just
996+
one is 1, the result is 1.
993997

994998
Although no warning is currently raised, the results are not well
995999
defined when these operations are performed on operands that aren't either
@@ -1488,14 +1492,12 @@ expressions. It's equivalent to C<&&> except for the very low
14881492
precedence. This means that it short-circuits: the right
14891493
expression is evaluated only if the left expression is true.
14901494

1491-
=head2 Logical or and Exclusive Or
1492-
X<operator, logical, or> X<operator, logical, xor>
1493-
X<operator, logical, exclusive or>
1494-
X<or> X<xor>
1495+
=head2 Logical Or
1496+
X<operator, logical, or> X<or>
14951497

14961498
Binary C<"or"> returns the logical disjunction of the two surrounding
1497-
expressions. It's equivalent to C<||> except for the very low precedence.
1498-
This makes it useful for control flow:
1499+
expressions. It's equivalent to C<||> except for it having very low
1500+
precedence. This makes it useful for control flow:
14991501

15001502
print FH $data or die "Can't write to FH: $!";
15011503

@@ -1517,11 +1519,22 @@ takes higher precedence.
15171519

15181520
Then again, you could always use parentheses.
15191521

1520-
Binary C<"xor"> returns the exclusive-OR of the two surrounding expressions.
1521-
It cannot short-circuit (of course).
1522-
15231522
There is no low precedence operator for defined-OR.
15241523

1524+
=head2 Logical Exclusive Or
1525+
X<operator, logical, xor> X<operator, logical, exclusive or> X<xor>
1526+
1527+
Binary C<"xor"> returns the logical disjunction of the two surrounding
1528+
expressions. That means it returns 1 if either, but not both, are true.
1529+
It's equivalent to C<^> except for it having very low precedence. It
1530+
tends to be used to verify that two mutually-exclusive conditions are
1531+
actually mutually exclusive. For example, in Perl's test suite, we
1532+
might want to test that a regular expression pattern can't both match
1533+
and not match, for otherwise it would be a bug in our pattern matching
1534+
code.
1535+
1536+
($a =~ /$pat/ xor $a !~ /$pat/) or die;
1537+
15251538
=head2 C Operators Missing From Perl
15261539
X<operator, missing from perl> X<&> X<*>
15271540
X<typecasting> X<(TYPE)>

0 commit comments

Comments
 (0)