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

Conversation

khwilliamson
Copy link
Contributor

Fixes #18565

  • This set of changes does not require a perldelta entry.

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.
Copy link
Contributor

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)?

Copy link
Contributor Author

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
@khwilliamson
Copy link
Contributor Author

In re-reading the changes, I realized the old text wrongly used "binary" instead of "logical". Also that I had wrongly said xor is the equivalent of ^. It instead is the equivalent of ^^ (which I hadn't known existed).

@@ -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.

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.

Copy link
Contributor

@Grinnz Grinnz left a 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

@Grinnz
Copy link
Contributor

Grinnz commented May 14, 2025

Also that I had wrongly said xor is the equivalent of ^. It instead is the equivalent of ^^ (which I hadn't known existed).

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;
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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[doc] perlop: splitting of the section about 'or' and 'xor'
4 participants