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

Re^3: Returning undef: The point I would like Damian to reconsider

by merlyn (Sage)
on Jun 22, 2007 at 22:22 UTC ( [id://622921]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Returning undef: The point I would like Damian to reconsider
in thread Returning undef: The point I would like Damian to reconsider

then it should always return a "list", even in scalar context, i.e. if called in scalar context return the last value in its return expression.
Ugh, that's not possible. You cannot return a list in a scalar context. Ever.

You can type:

return (99, 100, 101);
But that's not "returning a list in a scalar context", even if called in a scalar context. It's returning a scalar in a scalar context, by evaluating that comma-expression in a scalar context, getting the result 101.

I know you know that, but sloppy language here gets us running around in circles.

See my On Scalar Context for samples of many things that "listy" things do in a scalar context. Based on that, I don't see how "last thing" is any more intuitive than "first thing" or "number of elements" (the three main candidates).

  • Comment on Re^3: Returning undef: The point I would like Damian to reconsider
  • Download Code

Replies are listed 'Best First'.
Re^4: Returning undef: The point I would like Damian to reconsider
by jdporter (Paladin) on Jun 23, 2007 at 00:46 UTC

    WTF (that's "why") do you think I put "list" in quotes?

    When I say "list", everyone (including you) understands what I'm talking about. Give it a rest.

    More importantly, let's stick to the topic at hand. I intend to.

      Well, I don't know about "everybody", but I get the impression that you are under the all-too-common but still mistaken impression that "arrays return their size in a scalar context while all lists return their last item in a scalar context".

      I was rather surprised and disappointed to see this flawed meme show up at PerlMonks several times recently, and from people who I expected to know better.

      But I guess that is what happens when we "give it rest" even after having trounced that myth repeatedly and in no small detail for so long. *shrug*

      - tye        

        I get the impression that you ... "... all lists return their last item in a scalar context"

        If I gave that impression, I certainly didn't intend to. I'm well aware that many "things we normally use in a list context" return an interesting variety of other things in scalar context. Note, I said that, imho, returning the last value when called in scalar context is the minimally surprising behavior. I can also see the validity of the opinion that returning the count (length) might be the minimally surprising behavior. Any behavior other than those two would, I believe, be entirely too surprising and should be avoided. Obviously, in any case, the behavior should be documented, particularly for public interfaces.

        ...what happens when we "give it rest" even after having trounced that myth repeatedly...

        You're talking about a different thing. What I'm suggesting that merlyn give a rest is a purely terminological argument.

        A word spoken in Mind will reach its own level, in the objective world, by its own weight

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-23 23:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found