Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by ikegami (Patriarch)
on Dec 18, 2019 at 20:33 UTC ( [id://11110346]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^2: 'xor' operator is not a sibling to 'or' and 'and'?
by rsFalse (Chaplain) on Dec 18, 2019 at 20:43 UTC
    >> 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.

        In perlop I find that 'Binary "and" returns the logical conjunction of the two surrounding expressions.', not 'the last expression evaluated'. So maybe I misinterpret here? Or maybe text of docs here needs to be improved.
      > 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

        >> Binary "and" returns the logical conjunction of the two surrounding expressions.

        It's not much about short-circuit, but more about logical return value? I understand 'logical' as being Boolean, i.e. 1 or ''/0. So if an operator doesn't return a boolean value then this should be emphasized, and the sentence about return value rather should be corrected.

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11110346]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found