Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: What should be returned in scalar context?

by duff (Vicar)
on Dec 02, 2003 at 15:56 UTC ( #311658=note: print w/ replies, xml ) Need Help??


in reply to What should be returned in scalar context?

I don't think there should be a "standard". That's why this particular feature is in the hands of the programmer; so that they can make the decision about what's best. Sometimes you want to return the first element of an array in scalar context, sometimes you want to return something else.

Why do you think that there should be some standard?

Also, you have completely ignored void context. wantarray returns 3 things you know. Do you think that there should be some "standard" for what to do in void context too?


Comment on Re: What should be returned in scalar context?
Re: Re: What should be returned in scalar context?
by tilly (Archbishop) on Dec 02, 2003 at 16:14 UTC
    I am not talking a standard that is imposed on the programmer.

    I am saying that I as a programmer need to make this choice fairly often, what choices should I tend to make, and why?

    As for what to do in void context, my default has always been to treat it like scalar, but some people have already said that they standardizing on having functions mutate the input in place in void context. So yes, thinking through void context as well might make sense. (Though I probably won't because the only suggestion that I have seen runs too far counter to the rest of my personal style.)

      I am saying that I as a programmer need to make this choice fairly often, what choices should I tend to make, and why?

      You use wantarray that much? I've used it less than a couple of handfuls of times in the 11+ years I've been using perl. Most of the time I write subroutines that return lists, I say that's what they do. Period. It's rare that I need different functionality depending on context. It's only when I think "it sure would be nice if this routine did this sometimes and that other times" that wantarray might come into the picture. And when that happens, I've already decided what I need from scalar, list and void contexts. (I don't usually care about void context though)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2014-07-29 11:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (215 votes), past polls