Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by jdporter (Paladin)
on Jun 22, 2007 at 21:57 UTC ( [id://622917]=note: print w/replies, xml ) Need Help??


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

In the past, when faced with this question, I've usually gone on the principle that if a function normally/ever gets called in list context, 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. This, imho, is the minimally surprising behavior in most scenarios, and is also easiest to code as you don't have to code for sensitivity to calling context. OTOH, obviously there may be times when you'd rather have some other behavior; the only other one you should "reasonably" implement is having it act like an array, i.e. return the return list length when called in scalar context. But even that would warrant documentation including several exclamation points. Any other behaviors should, I think, be limited strictly to internal function calls, not in a public interface.

A word spoken in Mind will reach its own level, in the objective world, by its own weight
  • Comment on Re^2: Returning undef: The point I would like Damian to reconsider

Replies are listed 'Best First'.
Re^3: Returning undef: The point I would like Damian to reconsider
by merlyn (Sage) on Jun 22, 2007 at 22:22 UTC
    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).

      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        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-19 20:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found