Skip to content

Commit ea6e64f

Browse files
committed
perlop: Add detail about xor
And, note that 'or' is "inclusive"; 'xor' is "exclusive" And, explicitly give the results of bitwise '|' vs '^' And, fix the incorrect use of 'binary' when 'logical' was meant. Fixes #18565
1 parent b365cc5 commit ea6e64f

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

pod/perlop.pod

Lines changed: 30 additions & 12 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

1496-
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:
1498+
Logical C<"or"> returns the logical inclusive disjunction of the two
1499+
surrounding expressions. It's equivalent to C<||> except for it having
1500+
very low 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+
Logical C<"xor"> returns the logical exclusive disjunction of the two
1528+
surrounding expressions. That means it returns C<true> if either, but
1529+
not both, are true. It's equivalent to C<^^> except for it having very
1530+
low precedence. It tends to be used to verify that two
1531+
mutually-exclusive conditions are actually mutually exclusive. For
1532+
example, in Perl's test suite, we might want to test that a regular
1533+
expression pattern can't both match and not match, for otherwise it
1534+
would be a bug in our pattern matching code.
1535+
1536+
($a =~ qr/$pat/ xor $a !~ qr/$pat/) or die;
1537+
15251538
=head2 C Operators Missing From Perl
15261539
X<operator, missing from perl> X<&> X<*>
15271540
X<typecasting> X<(TYPE)>
@@ -3836,6 +3849,11 @@ Here is a short, but incomplete summary:
38363849

38373850
Choose wisely.
38383851

3852+
=head2 Logical or and Exclusive Or
3853+
3854+
This section has been split into L</Logical Or> and
3855+
L</Logical Exclusive Or>.
3856+
38393857
=head1 APPENDIX
38403858

38413859
=head2 List of Extra Paired Delimiters

0 commit comments

Comments
 (0)