-
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
Conversation
pod/perlop.pod
Outdated
X<operator, logical, xor> X<operator, logical, exclusive or> X<xor> | ||
|
||
Binary C<"xor"> returns the logical disjunction of the two surrounding | ||
expressions. That means it returns 1 if either, but not both, are true. |
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.
Does it return 1
or true
(the boolean)?
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.
true
. Fixed
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 Perl#18565
In re-reading the changes, I realized the old text wrongly used "binary" instead of "logical". Also that I had wrongly said |
@@ -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 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.
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.
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.
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.
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.
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 |
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.
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 think or and xor should be kept as the same section, and the "binary" term should not be changed, otherwise LGTM
It had not existed, until the 5.40 release. https://perldoc.perl.org/perl5400delta#New-%5E%5E-logical-xor-operator |
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid promoting use of $a
in docs
($a =~ qr/$pat/ xor $a !~ qr/$pat/) or die; | |
($x =~ qr/$pat/ xor $x !~ qr/$pat/) or die; |
Fixes #18565