|
|
Subscribe / Log in / New account

Logical XOR

Logical XOR

Posted Jun 10, 2024 16:08 UTC (Mon) by rweikusat2 (subscriber, #117920)
Parent article: perl v5.40.0 released

^^ is the XOR equivalent to || and &&. Does anybody have an idea what that could be good for? I've certainly never missed it.


to post comments

Logical XOR

Posted Jun 10, 2024 16:38 UTC (Mon) by proski (subscriber, #104) [Link]

I wonder if the quote in the announcement email is inspired by the "cat operator" ^^

Logical XOR

Posted Jun 10, 2024 16:53 UTC (Mon) by jccleaver (guest, #127418) [Link] (7 responses)

As syntactic sugar, it's helpful for clearing up a few logic flows. While it'll be a while before one could say this will be *clearer*, once folks are familiar with it it will let you make more legible lines.

Logical XOR

Posted Jun 10, 2024 17:19 UTC (Mon) by pbonzini (subscriber, #60935) [Link] (6 responses)

But why can't you use == or != for logical XNOR and XOR respectively? XOR and XNOR can't be short-circuited anyway.

Logical XOR

Posted Jun 10, 2024 18:20 UTC (Mon) by smurf (subscriber, #17840) [Link] (3 responses)

Comparison isn't sufficient, you also need to bool-ize one or both values. The Perl-ish way of writing "x xor y" was "!x != !y" which is … meh.

On the other hand, I haven't programmed anything Perl-ish for the last 15 years, so what do I know?

Logical XOR

Posted Jun 10, 2024 19:33 UTC (Mon) by mathstuf (subscriber, #69389) [Link]

Why not learn something from Haskell and make it applicative so you can say something like `!*(x = y)` that applies the `!` operator to each token in the inside of the parenthesized expression? Of course, being Perl, maybe this will be adopted too ;) . Or it already has meaning?

Logical XOR

Posted Jun 10, 2024 20:21 UTC (Mon) by rweikusat2 (subscriber, #117920) [Link] (1 responses)

Bool-ize is a bit of a misnomer as Perl has no boolean data type. The value has to be normalized to something where a numerical comparison (!=) works as intended because non-numerical strings are autoconverted to the integer 0 when used in an numerical comparison. In Perl, this expression

0 != "a"

is false but

!0 != !"a"

is true.

Logical XOR

Posted Jun 11, 2024 19:22 UTC (Tue) by jwilk (subscriber, #63328) [Link]

> Perl has no boolean data type

Since v5.36, it kinda has:

https://perldoc.perl.org/perl5360delta#Stable-boolean-tra...

Logical XOR

Posted Jun 10, 2024 18:25 UTC (Mon) by rweikusat2 (subscriber, #117920) [Link]

a ^^ b is equivalent to (a || b) && !(a && b). In Perl, (a || b) && !(a && b) && (a == b) is possible:

perl -e '$a = 0; $b = 'abc'; print(($a || $b) && !($a && $b) && ($a == $b), "\n")'

:-)

Logical XOR

Posted Jun 11, 2024 22:58 UTC (Tue) by MarcB (guest, #101804) [Link]

It is not really the logical XOR that is new. Perl already had this, named "xor". But "xor" is a low-precedence operator, like "and" or "or" (vs. "&&" or "||"). The high-precedence version of "xor" was missing before, which was an inconsistency.

That is all this change is about. Now all logical operator exist in three versions: bit-wise, high-precedence logical and low-precedence logical.

See https://perldoc.perl.org/perlop#Operator-Precedence-and-A...

Logical XOR

Posted Jun 10, 2024 17:04 UTC (Mon) by mb (subscriber, #50428) [Link]

> ^^ is the XOR equivalent to || and &&. Does anybody have an idea what that could be good for?

Well, that's easy.
If you think coding is fun, use ^^

¯\_(ツ)_/¯

C-Style Logical XOR

Posted Jun 10, 2024 19:22 UTC (Mon) by sub2LWN (subscriber, #134200) [Link] (7 responses)

In the "perlop" documentation it is called a "C-Style Logical Xor," so one use could be to encourage (Perl∩C) programmers to (re)visit the documentation of either language.

C-Style Logical XOR

Posted Jun 10, 2024 21:18 UTC (Mon) by dskoll (subscriber, #1630) [Link] (6 responses)

Oof, C does not have a logical XOR, so calling it "C-style" is a bit of a stretch. I can't recall ever needing a logical XOR, but if I did, I would be perfectly fine with writing (A && !B) || (B && !A). And yes, I know the parens are not strictly necessary, but I like them.

I suppose if A and B had side-effects, that might be a problem, but just Don't Do That.

C-Style Logical XOR

Posted Jun 11, 2024 4:20 UTC (Tue) by willy (subscriber, #9762) [Link] (5 responses)

I use bitops regularly and very rarely need to use ^. I'd argue it's a wart of C to use one of the rare single character operators for this purpose. I'd like to be able to write "X xor Y", but 2^30 is commonly understood to mean 1<<30 and not 28 as the C parser interprets it. Even "a = pi * r^2" would normally be interpreted differently by a human and a C parser.

C-Style Logical XOR

Posted Jun 11, 2024 16:37 UTC (Tue) by dskoll (subscriber, #1630) [Link] (4 responses)

Yup, xor is not often used in general-purpose software, though it is used in quite a few encryption and hashing algorithms. But that's bitwise-xor. I'm scratching my head trying to come up with a scenario where logical-xor is needed.

I agree that reserving ^ for exponentiation would have been better than reserving it for xor, but that's water under the bridge.

C-Style Logical XOR

Posted Jun 11, 2024 17:52 UTC (Tue) by NYKevin (subscriber, #129325) [Link]

I think I have occasionally wanted logical XNOR in general programming (I don't write much/any Perl), but I usually just end up writing bool(x) == bool(y) (replacing bool() with whatever the language's coerce-to-booleanish syntax looks like, or omitting it if we're in a strongly-typed language that has first-class bools). IMHO that's easier to read than a hypothetical XOR operator would be anyway. I suppose it might be useful in bash-like languages, where you'd have to screw around with $? and temporary variables to write an XOR/XNOR using equality. But no bash-like language actually has this feature (that I know of).

C-Style Logical XOR

Posted Jun 11, 2024 23:04 UTC (Tue) by MarcB (guest, #101804) [Link]

I recall using it once, for configuration validation where options were mutually exclusive. But it really is rare.

C-Style Logical XOR

Posted Jun 14, 2024 15:57 UTC (Fri) by magnus (subscriber, #34778) [Link] (1 responses)

If you're writing code to draw a chess board and deciding which color to use in each square, logical xor could be handy (row number is even xor column number is even). But thats a bit of a niche case :)

C-Style Logical XOR

Posted Jun 14, 2024 16:48 UTC (Fri) by atnot (subscriber, #124910) [Link]

Surely in that case you'd usually rather write something such as `col ^ row & 1`? It's kind of a bit too smart, but not much more than using an xor operator.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds