-
Notifications
You must be signed in to change notification settings - Fork 577
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
base: blead
Are you sure you want to change the base?
perlop: Add detail about xor #23286
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
|
@@ -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 | ||||||
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: $!"; | ||||||
|
||||||
|
@@ -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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid promoting use of
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)> | ||||||
|
@@ -3836,6 +3849,11 @@ Here is a short, but incomplete summary: | |||||
|
||||||
Choose wisely. | ||||||
|
||||||
=head2 Logical or and Exclusive Or | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the precedence table at the top, |
||||||
|
||||||
This section has been split into L</Logical Or> and | ||||||
L</Logical Exclusive Or>. | ||||||
|
||||||
=head1 APPENDIX | ||||||
|
||||||
=head2 List of Extra Paired Delimiters | ||||||
|
There was a problem hiding this comment.
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-
.There was a problem hiding this comment.
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.