Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

'xor' operator is not a sibling to 'or' and 'and'?

by rsFalse (Hermit)
on Dec 18, 2019 at 20:29 UTC ( #11110345=perlquestion: print w/replies, xml ) Need Help??

rsFalse has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I have used 'xor' operator as lower precedence comma, because I understood a logic that it cannot short-circuit as do 'or' and 'and', and the last expression must be evaluated at the end. E.g. I used 'xor' in short sentences like this, to avoid unnecessary parentheses:
#!/usr/bin/perl -l use strict; use warnings; push @_, $_ xor print $_ for 'a' .. 'b'; print @_;
Output:
Useless use of logical xor in void context at ./perlmonks_xor_vs_or_an +d.pl line 6. a b ab
Today I found (for myself) that 'xor' doesn't work in the same way as 'or' and 'and' do, and it is strange for me, why. It seems that, 'xor' not only evaluates one or both expressions surrounding it, but also evaluates logical XOR of these expressions, when 'or' and 'and' operators don't do this, i.e.:
#!/usr/bin/perl -l use strict; use warnings; print "[$_]" for ( 2 and 3 ), ( 2 or 3 ), ( 2 xor 3 ), ;
Output:
[3] [2] []
At the last line of output I expected to get '3'!
I could get '3' by using a comma instead (and forcing scalar context): 'scalar( 2, 3 ),'.
I'm not sure this difference of behaviour of operators is written somewhere in documentation, but I haven't find it in 'perlop'.

Replies are listed 'Best First'.
Re: 'xor' operator is not a sibling to 'or' and 'and'?
by GrandFather (Sage) on Dec 18, 2019 at 21:29 UTC

    xor can only return true (1) or false ('' - empty string) and must evaluate both the lhs and the rhs expressions to determine the result. true xor true is false so you got back an empty string.

    and and or short circuit evaluate and return the last expression evaluated.

    use strict; use warnings; print "$_->[0]: [$_->[1]]\n" for map {[$_, eval]} '0 and 1', '2 and 0', '3 and 4', '0 or 5', '6 or 0', '7 or 8', '0 xor 9', '10 xor 0', '11 xor 12', '0 xor 0', ;
    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: 'xor' operator is not a sibling to 'or' and 'and'?
by ikegami (Pope) on Dec 18, 2019 at 20:33 UTC

    It seems that, 'xor' not only evaluates one or both expressions surrounding it, but also evaluates logical XOR of these expressions, when 'or' and 'and' operators don't do this

    huh? That's exactly what or and and do (aside from evaluating logical OR and AND respectively, of course).

    At the last line of output I expected to get '3'!

    TRUE XOR TRUE = FALSE, so 2 xor 3 can't possibly return 3, a true value.

      >> TRUE XOR TRUE = FALSE, so 2 xor 3 can't possibly return 3.

      But in the same way 'and' operator does not output '1', despite TRUE AND TRUE = TRUE.

        and and or return the last expression evaluated; xor always evaluates both sides and then returns the exclusive or of those two expressions (not either of the surrounding expressions) so it's always going to be a boolean value either 1 or ''/0. See perlop

        Update: On further reflection I think I see what your expectations for the behavior might be based on and/or's behavior: if A xor B is true return whichever of A or B was true, otherwise return false.

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

        > But in the same way and operator does not output '1', despite TRUE AND TRUE = TRUE.

        That's the feature of short circuit and and or which can't be replicated with xor

        Grandfather and ikegami already explained it perfectly.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        No, that's completely wrong. The reason $TRUE and $TRUE doesn't necessarily return 1 has nothing to do with why $TRUE xor $TRUE doesn't return 1.

        $TRUE and $TRUE doesn't necessarily return 1 because it's more useful to return its RHS.

        $TRUE xor $TRUE doesn't return 1 because that would be wrong.

Re: 'xor' operator is not a sibling to 'or' and 'and'? (short-circuit and other languages.)
by LanX (Cardinal) on Dec 19, 2019 at 17:53 UTC
    This WP article: Short-circuit evaluation

    covers most if not all aspects of this thread and lists languages with similar behavior. (See table entries marked "last value", like Perl, Ruby, JS, Python,...)

    Quotes:

    • In loosely typed languages that have more than the two truth-values True and False, short-circuit operators may return the last evaluated subexpression. The expression x and y is equivalent to y if x else x; the expression x or y is equivalent to x if x else y (without evaluating x twice). This is called "last value" in the table below.
    • For some Boolean operations, like exclusive or (XOR), it is not possible to short-circuit, because both operands are always required to determine the result.

    see also Re^5: 'xor' operator is not a sibling to 'or' and 'and'?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: 'xor' operator is not a sibling to 'or' and 'and'?
by BillKSmith (Prior) on Dec 18, 2019 at 22:55 UTC
    These operators are called 'Logical' for a reason. They are meant to return a logical value (true or false). Code that depends on the string or numeric value of the result, which arguments are evaluated, or even in the order in which they are evaluated could fail under a future version of perl. Note that the idiom open ... or die "..."; does depend on the open being done first, but is so common I am sure it will always be supported.
    Bill

      I can't imagine the expression result behavior of Perl's logical operators ever going away. There is a large amount of Perl idiom that bakes in that behavior so huge amounts of code would break if that changed. I'm sure the behavior isn't an accidental result of a weird coding decision, but was fully thought out with a view to allowing nice fall back processing using the logical or operators.

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11110345]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2020-10-24 00:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (241 votes). Check out past polls.

    Notices?