Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

return ref is better ?

by odrevet (Acolyte)
on Feb 06, 2010 at 21:30 UTC ( [id://821776]=perlquestion: print w/replies, xml ) Need Help??

odrevet has asked for the wisdom of the Perl Monks concerning the following question:

Hi !
I am too greedy to buy perl best practice book. So in a nutshell, is it better in a subroutine to return a ref or a regulare array/hash is just fine ?

Replies are listed 'Best First'.
Re: return ref is better ?
by jethro (Monsignor) on Feb 06, 2010 at 23:42 UTC

    I didn't check the Perl Best Practices book, but I would suspect that it says nothing about that. Because depending on the situation one or the other might be better.

    Usually a ref is better for really big arrays or hashes or when you want to get a specific array or hash and not a copy. In most other cases the regular array/hash is just fine as it allows for simpler syntax

Re: return ref is better ?
by dsheroh (Monsignor) on Feb 07, 2010 at 10:40 UTC
    Depends on the situation. Refs are more efficient when you're dealing with larger datasets, but less comfortable for people who aren't as accustomed to dealing with them.

    My personal practice is to always return refs because it lets me concentrate on more important things than trying to remember which functions return refs and which return arrays/hashes. Always returning arrays/hashes would have this same benefit, of course, but doesn't allow for returning multiple array/hash results.

    And then there's the option of using wantarray to return refs in scalar context and arrays/hashes in list context. In theory, it seems like it could provide the best of both worlds by letting the caller get the result in whatever form they want, but it's not a habit I've cultivated. And it also doesn't work for returning multiple array/hash results.

Re: return ref is better ?
by YuckFoo (Abbot) on Feb 06, 2010 at 22:37 UTC
    I like returning refs, but am too greedy to say why.
Re: return ref is better ?
by tilly (Archbishop) on Feb 07, 2010 at 21:09 UTC
    There is no best solution, and having to always think about it complicates virtually every API.

    In general if you get into the whole map, grep, etc frame of mind then returning lists is better than refs. Returning refs is marginally more efficient and better fits how some other people work. That also allows you to keep your style more similar as you move between Perl and other languages. The whole wantarray and return a ref or a single element seems like a good idea, but my experience with it has not been very good.

    So any choice you make can be defended.

Re: return ref is better ?
by Khen1950fx (Canon) on Feb 06, 2010 at 21:41 UTC
    Maybe this will help.

      Note that the referenced link discusses prototyping without officially frowning upon it, thus, at least by implication, endorsing it.

      It also invokes all of its example subroutines with a  & sigil, another frowned-upon practice. I only spent a couple of minutes reviewing the material, but I got the feeling it is a mixture of good and bad advice, with no way for the novice to tell which is which.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-04-16 10:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found