Discussion:
Why doesn't Perl have the ^^ operator?
(too old to reply)
Windl, Ulrich
2024-02-07 11:39:57 UTC
Permalink
Here is a problem:
I have a selftest that goes like "if ($obj->method()) { fine() } else {bad() }" I id coverd that depending on the version of some external module, the logic may be inverted, because the test should have failed actually if the module weren't broken
So to fix, I added a variable Boolean $broken, and the logic would be "if ($broken && $obj->method() || $broken && ! $obj->method()) { fine() } else {bad() }", obviously an XOR. However as I don't really know whether the method returns a number, I cannot use the ^ (bitwise) operator, and abviously there is no ^^ logical operator.

Here is the syntax that I'm proposing:
if (!($broken ^^ $obj->method())) { fine() } else {bad() }" (if I got the logic right)

Here are the benefits of this:
With & and &&, | and ||, (~ and !) that just seems logical to have ^and ^^.

Here are potential problems:
I don't see any potential problems with that.
Darren Duncan
2024-02-09 04:42:55 UTC
Permalink
I have a selftest that goes like “if ($obj->method()) { fine() } else {bad()
}” I id coverd that depending on the version of some external module, the
logic may be inverted, because the test should have failed actually if the
module weren’t broken
So to fix, I added a variable Boolean $broken, and the logic would be “if
($broken && $obj->method() || $broken && ! $obj->method()) { fine() } else
{bad() }”, obviously an XOR. However as I don’t really know whether the method
returns a number, I cannot use the ^ (bitwise) operator, and abviously there
is no ^^ logical operator.
Logical XOR is just not-equals on booleans. Your proposed A ^^ B can be written
as (!A) != (!B) today.
I agree xor is just not-equals on booleans, as xnor is equals on booleans.

I see 2 main things the explicit operator would gain us:

1. It would tersely cast the arguments as booleans, bringing parity with ne
doing it to strings and != doing it to numbers.

2. It would bring design parity with the set of bitwise operators, for what
that's worth.

But in the absence of this, an explicit cast plus !+ isn't a terrible option.

(In some ways supporting xor as a distinct operator is kind of arbitrary
considering even in languages having it they are typically omitting most of the
other dyadic boolean operators anyway, of which there are 10 total.)

-- Darren Duncan
Darren Duncan
2024-02-09 20:19:20 UTC
Permalink
It would make sense for Perl to have ^^ but not the super-low-
precedence xor version. Low-precedence or and and are useful for joining
commands together conditionally, where the result of the first command
determines whether the second is run. With xor that's nonsense, because
both operands have to be evaluated anyway.
(Obviously I'm not suggesting removing xor, because it does exist and it
isn't causing any harm. But it's hard to see how it's useful with low
precedence.)
From a design perspective, I disagree with you. There should be full parity
where each logical operator has a low and high precedence version. The existing
lower priority xor is no less valuable than the proposed higher priority
symbolic version. -- Darren Duncan
Windl, Ulrich
2024-02-09 06:45:16 UTC
Permalink
Hi!

OK, I didn't know that. Actually I had implemented my own XOR() in the meantime. Still ^^ would make sense for consistency IMHO

Ulrich

-----Original Message-----
From: Tomasz Konojacki <***@xenu.pl>
Sent: Friday, February 9, 2024 6:27 AM
To: Windl, Ulrich <***@ukr.de>
Cc: perl5-***@perl.org
Subject: [EXT] Re: Why doesn't Perl have the ^^ operator?

On Wed, 7 Feb 2024 11:39:57 +0000
Post by Windl, Ulrich
I have a selftest that goes like "if ($obj->method()) { fine() } else {bad() }" I id coverd that depending on the version of some external module, the logic may be inverted, because the test should have failed actually if the module weren't broken
So to fix, I added a variable Boolean $broken, and the logic would be "if ($broken && $obj->method() || $broken && ! $obj->method()) { fine() } else {bad() }", obviously an XOR. However as I don't really know whether the method returns a number, I cannot use the ^ (bitwise) operator, and abviously there is no ^^ logical operator.
if (!($broken ^^ $obj->method())) { fine() } else {bad() }" (if I got the logic right)
With & and &&, | and ||, (~ and !) that just seems logical to have ^and ^^.
I don't see any potential problems with that.
perl -Mwarnings -E 'if(undef ^ 1) { say 1 }'
Use of uninitialized value in numeric bitwise xor (^) at -e line 1.
1
Post by Windl, Ulrich
perl -Mwarnings -E 'if(undef xor 1) { say 1 }'
1
Paul "LeoNerd" Evans
2024-02-13 16:02:23 UTC
Permalink
I don't think there's any particular design or reason why we don't have
a ^^ operator, we've just never really needed it or got around to
adding one.

It's unlikely to get in for 5.40, but if anyone feels strongly that we
should have such an operator, make a branch and add some tests and
docs, and I can write the actual code to do it. Then we'll be in a
position to think about merging it or not.
--
Paul "LeoNerd" Evans

***@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/
Martijn Lievaart via perl5-porters
2024-02-13 22:06:32 UTC
Permalink
Post by Paul "LeoNerd" Evans
I don't think there's any particular design or reason why we don't have
a ^^ operator, we've just never really needed it or got around to
adding one.
It's unlikely to get in for 5.40, but if anyone feels strongly that we
should have such an operator, make a branch and add some tests and
docs, and I can write the actual code to do it. Then we'll be in a
position to think about merging it or not.
Tests done, docs still todo, but when done, can I just push that branch
or do I need to get some permissions first?


M4
James E Keenan
2024-02-13 22:48:31 UTC
Permalink
Post by Martijn Lievaart via perl5-porters
Post by Paul "LeoNerd" Evans
I don't think there's any particular design or reason why we don't have
a ^^ operator, we've just never really needed it or got around to
adding one.
It's unlikely to get in for 5.40, but if anyone feels strongly that we
should have such an operator, make a branch and add some tests and
docs, and I can write the actual code to do it. Then we'll be in a
position to think about merging it or not.
Tests done, docs still todo, but when done, can I just push that branch
or do I need to get some permissions first?
Best to push a branch to your own GH repository and make a pull request.
Paul "LeoNerd" Evans
2024-02-15 21:59:33 UTC
Permalink
On Tue, 13 Feb 2024 23:06:32 +0100
Post by Martijn Lievaart via perl5-porters
Post by Paul "LeoNerd" Evans
I don't think there's any particular design or reason why we don't
have a ^^ operator, we've just never really needed it or got around
to adding one.
It's unlikely to get in for 5.40, but if anyone feels strongly that
we should have such an operator, make a branch and add some tests
and docs, and I can write the actual code to do it. Then we'll be
in a position to think about merging it or not.
Tests done, docs still todo, but when done, can I just push that
branch or do I need to get some permissions first?
Tests and docs were provided, and now I have written an actual
implementation.

Draft PR here:

https://github.com/Perl/perl5/pull/21996
--
Paul "LeoNerd" Evans

***@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/
Loading...