Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Re: Re: What should be returned in scalar context? (best practice)

by ysth (Canon)
on Dec 21, 2003 at 17:42 UTC ( [id://316221]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: What should be returned in scalar context? (best practice)
in thread What should be returned in scalar context?

But if you say return @ret you aren't returning a list when called in scalar context. You have to say return @ret[0..$#ret] for that.

Replies are listed 'Best First'.
Re: Re: Re: Re: What should be returned in scalar context? (best practice)
by Juerd (Abbot) on Dec 21, 2003 at 18:00 UTC

    But if you say return @ret you aren't returning a list when called in scalar context.

    While that is true, it is *never* returning an array in any context. At least when you say list, that's some indication you should use the sub in list context :)

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re^4: What should be returned in scalar context? (best practice)
by sauoq (Abbot) on Feb 25, 2011 at 20:56 UTC

    Seven years later I come to this thread and wonder why what you said here got no real attention.

    And seven years later, I still agree with (nearly) everything I myself said in this thread.

    Pedantry be damned. It's nice enough ammo for meaningless flame wars on sites like this one but when people are actually working together on a common goal and not pulling out their micrometers for a geek pecker measuring contest, I've learned that many of them are too smart to bother being pedantic.

    Thank heavens for that.

    Anyway, I'm still saying "returns an array" and everything is moving along as smooth as I might hope. Turns out that telling people what I am doing works just fine. (They've already learned what Perl does.)

    And here was the crux of it all along. When I return an array (sub foo { @a = qw/2 4 6 8/ }) it simply doesn't behave the same as when I return a list (sub bar { qw/2 4 6 8/ }). Go figure. And with my convenient shorthand, I don't have to explain about this context or that one and get into petty details unless I'm doing something strange. Like using wantarray.

    Makes you wonder if Perl was slapped together by a some crazy linguist or something.

    -sauoq
    "My two cents aren't worth a dime.";

      Ditto. (Long time no see. How's tricks?:)

        Going well. How are you? Maybe we should take this to private messages. ;-)
        -sauoq
        "My two cents aren't worth a dime.";

Log In?
Username:
Password:

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

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

    No recent polls found