Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Truth and Falsehood

by ikegami (Patriarch)
on Aug 18, 2019 at 06:29 UTC ( [id://11104632]=note: print w/replies, xml ) Need Help??


in reply to Truth and Falsehood

The empty list (), which evaluates to undef.

When's the last time you did if (()) {}? Why would you ever do that?

I suppose one could use sub f { return (); } if (f()) {}, but that's a very bad practice. (A sub that's expected to return a scalar should do so even in list context.)

So I'm rather baffled as to why this was mentioned. There are so many operators that can return false that would merit a mention before (), such as scalar assignment, list assignment, grep, map, m// and s///.

Replies are listed 'Best First'.
Re^2: Truth and Falsehood
by haukex (Archbishop) on Aug 18, 2019 at 07:04 UTC
    I suppose one could use sub f { return (); } if (f()) {}, but that's a very bad practice. (A sub that's expected to return a scalar should do so even in list context.)

    I agree that subs that are expected to return a scalar should do return undef because of the problems with my %hash = ( k => f() );, but there are also some good arguments against return undef in other cases - as you've said, there are exceptions.

    I mostly mentioned it because the old section "Truth and Falsehood" mentioned it, although with an attempt to improve the wording, as you've suggested yourself in the past.

    Update: I missed your ninja edits, but I hope I've cleared up your bafflement nonetheless. Please feel free to add any information you think is missing!

Re^2: Truth and Falsehood
by jcb (Parson) on Aug 18, 2019 at 07:13 UTC

    Logically, operators like grep, map, m//, and s/// all return false by returning an empty list, at least in a simple description.

      That makes no sense. An empty list isn't false. Or true. It's not a scalar, so those concepts don't apply to it. You can't evaluate whether an empty list is true or false. You can only evaluate if a scalar is true or false.

      As such, if you're trying to get a true or false value from those operators, you would be evaluating them in scalar context, and the following is what the listed operators actually return in scalar context:

      • grep returns the count of matching elements.
      • map returns the count of elements it would return.
      • m// and s/// return success or not.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-25 20:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found