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

Re: Advantages to returning array vs. reference

by pjf (Curate)
on Oct 14, 2001 at 04:51 UTC ( #118722=note: print w/replies, xml ) Need Help??


in reply to Advantages to returning array vs. reference

Update: Fixed my link to previous discussions on the topic. Thanks to jptxs and Hopes for pointing out that I typoed on the link-id.

For previous discussions on this topic, see here.

In short, passing around references is much much faster, but can confuse people who are new to Perl, and may give your caller access to things they shouldn't if (say) you're passing back references to package variables.

Provided you don't have these problems, you may want to use wantarray to figure out what the caller is expecting. If they want a scalar, give them a reference. If they want a list, then return an array:

if (wantarray) { return @results; } else { return \@results; }
This adds a bit more DWIM-ism to your code, although it also runs the risk of confusing some people if they're expecting to get the number of results if they're calling your function in a scalar context.

Cheers,
Paul

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://118722]
help
Chatterbox?
[1nickt]: stevieb it turns out that Dell still has the Precision line of "mobile workstations", just not displayed on their main website. I found the Precision 7710 with 4k display/full-size keybd as req'd.
[1nickt]: List $2,788 (!), on sale for $1766, also a $200 coupon on the site thru 3/30 ... final price $1,566. Includes 3-yr on-site service.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (12)
As of 2017-03-27 12:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (320 votes). Check out past polls.