Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by rsFalse (Chaplain)
on Dec 18, 2019 at 21:11 UTC ( [id://11110353]=note: print w/replies, xml ) Need Help??


in reply to Re^3: 'xor' operator is not a sibling to 'or' and 'and'?
in thread 'xor' operator is not a sibling to 'or' and 'and'?

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.
  • Comment on Re^4: 'xor' operator is not a sibling to 'or' and 'and'?

Replies are listed 'Best First'.
Re^5: 'xor' operator is not a sibling to 'or' and 'and'?
by Fletch (Bishop) on Dec 18, 2019 at 21:23 UTC

    It's further down under the heading "Logical Defined-Or" (so yes, it's disjoint from the other discussion of && above it):

    The "||", "//" and "&&" operators return the last value evaluated +(unlike C's "||" and "&&", which return 0 or 1). Thus, a reasonably portab +le way to find out the home directory might be:

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

      Hm, paragraphs about '||,//,&&' and about 'and/or/xor' are distant in docs, because their precedence differs. But also sentences about return values of 'and' and 'or' seems to be incorrect. I think that section needs to be slightly rewritten, with emphasis that 'and' and 'or' do not return logical result, but return evaluated expressions.

        Stop thinking in terms of bitwise; this is Boolean. The "evaluated expressions" are logical values. 2 is logic-true, 3 is logic-true, 2 and 3 returns 3, which is logic-true. It uses the last evaluated expression as the logical value.

        C:\usr\local\share\PassThru\perl>perl -le "print +('george' and 'graci +e')" gracie C:\usr\local\share\PassThru\perl>perl -le "print +('0' and 'gracie')" 0 C:\usr\local\share\PassThru\perl>perl -le "print +('' and 'gracie')" C:\usr\local\share\PassThru\perl>perl -le "print +('george' and 'false +')" false C:\usr\local\share\PassThru\perl>perl -le "print +('george' and '')" C:\usr\local\share\PassThru\perl>perl -le "print +(2 and 4)" 4 C:\usr\local\share\PassThru\perl>perl -le "print +(2 && 4)" 4 C:\usr\local\share\PassThru\perl>perl -le "print +(2 & 4)" 0
        > I think that section needs to be slightly rewritten

        I agree that && and || should be extended with the notion: "returns last evaluated side".

        Probably with an analogous if statement.

        A || B := do { if (A) {A} else {B} }

        I disagree that they "do not return logical result" , because the truth table is fully respected.

        I don't think the docs to and/or need to be extend, because their low precedence and use for flow control makes the use of return values unlikely without brackets. Mentioning the similarity to && and || should be sufficient.

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (8)
As of 2024-04-18 15:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found