Skip to content

perlop: Add detail about xor #23286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: blead
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions pod/perlop.pod
Original file line number Diff line number Diff line change
Expand Up @@ -987,9 +987,13 @@ C<"experimental::bitwise"> category.
X<operator, bitwise, or> X<bitwise or> X<|> X<operator, bitwise, xor>
X<bitwise xor> X<^>

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

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

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

=head2 Logical or and Exclusive Or
X<operator, logical, or> X<operator, logical, xor>
X<operator, logical, exclusive or>
X<or> X<xor>
=head2 Logical Or
X<operator, logical, or> X<or>

Binary C<"or"> returns the logical disjunction of the two surrounding
expressions. It's equivalent to C<||> except for the very low precedence.
This makes it useful for control flow:
Logical C<"or"> returns the logical inclusive disjunction of the two
Copy link
Contributor

@Grinnz Grinnz May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think "binary" here refers to the operation, but to the syntax of the operator: it takes two arguments. Note that the versions of these operators marked as "bitwise" as well as the C-style logical forms are also introduced with this term. In fact it is distinguishing for example binary - with unary -.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right about the intent, based on looking at other portions of the document. But it confused me, and thus could easily confuse others; so some clarification is necessary.

surrounding expressions. It's equivalent to C<||> except for it having
very low precedence. This makes it useful for control flow:

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

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

Then again, you could always use parentheses.

Binary C<"xor"> returns the exclusive-OR of the two surrounding expressions.
It cannot short-circuit (of course).

There is no low precedence operator for defined-OR.

=head2 Logical Exclusive Or
X<operator, logical, xor> X<operator, logical, exclusive or> X<xor>

Logical C<"xor"> returns the logical exclusive disjunction of the two
surrounding expressions. That means it returns C<true> if either, but
not both, are true. It's equivalent to C<^^> except for it having very
low precedence. It tends to be used to verify that two
mutually-exclusive conditions are actually mutually exclusive. For
example, in Perl's test suite, we might want to test that a regular
expression pattern can't both match and not match, for otherwise it
would be a bug in our pattern matching code.

($a =~ qr/$pat/ xor $a !~ qr/$pat/) or die;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid promoting use of $a in docs

Suggested change
($a =~ qr/$pat/ xor $a !~ qr/$pat/) or die;
($x =~ qr/$pat/ xor $x !~ qr/$pat/) or die;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed


=head2 C Operators Missing From Perl
X<operator, missing from perl> X<&> X<*>
X<typecasting> X<(TYPE)>
Expand Down Expand Up @@ -3836,6 +3849,11 @@ Here is a short, but incomplete summary:

Choose wisely.

=head2 Logical or and Exclusive Or
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe these were put in the same section because the current organization of perlop is to document operators in the order of precedence, with operators of the same precedence (like or and xor) sharing a section.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it then the case that there is a distinct precedence order in && || ^^ // .. as these all have their own section in this order? My guess is that || and ^^ have the same precedence.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the precedence table at the top, && has a distinct precedence and || // ^^ share a precedence. Perhaps this was an oversight when adding the latter two operators.


This section has been split into L</Logical Or> and
L</Logical Exclusive Or>.

=head1 APPENDIX

=head2 List of Extra Paired Delimiters
Expand Down
Loading