Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Is it better to return a hash of hashes or a reference to a hash of hashes?

by uwevoelker (Pilgrim)
on Oct 08, 2001 at 19:15 UTC ( [id://117470]=perlquestion: print w/replies, xml ) Need Help??

uwevoelker has asked for the wisdom of the Perl Monks concerning the following question: (subroutines)

Is it better to return a hash of hashes or a reference to a hash of hashes?

Originally posted as a Categorized Question.

  • Comment on Is it better to return a hash of hashes or a reference to a hash of hashes?

Replies are listed 'Best First'.
Re: Is it better to return a hash of hashes or a reference to a hash of hashes?
by merlyn (Sage) on Oct 08, 2001 at 19:20 UTC
    References are a single item, while lists are the entire list. In terms of speed, it's more expensive to pass a list than an single item. However, "better" is a relative term, so without more context...
Re: Is it better to return a hash of hashes or a reference to a hash of hashes?
by Aristotle (Chancellor) on Oct 09, 2001 at 11:14 UTC

    Another criterion to use is whether you are returning "final" or "intermittent" data: that is to say, if you just collected results in your subroutine (say, a DBI query), then returning a reference is probably the best idea most of the time. If, however, the data is a persistent part of an object, it is probably unwise to return a reference for several reasons. For one, you are giving your caller full access to the data; for another, some child class may choose to store data differently, so returning a hash reference is no longer a "natural" way to go about the query.

Re: Is it better to return a hash of hashes or a reference to a hash of hashes?
by Russ (Deacon) on Mar 05, 2002 at 20:04 UTC
    In the final analysis, it is up to you. One of the strengths of Perl is how it allows the programmer to work efficiently. The computer usually works efficiently, too, but the programmer's efficiency comes first.

    So, if you decide it is better/easier/simpler to return an "expensive" data structure (rather than forcing your caller to dereference it later), then go right ahead! Unless you are doing something really "out there" (and I could give you some examples I have tried and discarded ;-) Perl will take good care of you.

Re: Is it better to return a hash of hashes or a reference to a hash of hashes?
by Juerd (Abbot) on Mar 07, 2002 at 07:38 UTC
    One thing I'd like to add is that returning a hash reference allows for beatiful syntax when you're using a single element from it.
    sub hash { qw(foo bar baz xyzzy) } sub hashref { return { qw(foo bar baz xyzzy) } } my %dummy = hash; $dummy = $dummy{foo}; # Afaik, there is no direct syntax to get a single element # but with hash refs, there is: $dummy = hashref->{foo} # equals $dummy = ${ hashref() }{foo}, which is ugly.
Re: Is it better to return an hash of hashes or a reference to an hash of hashes?
by Juerd (Abbot) on Mar 06, 2002 at 18:42 UTC
    One thing I'd like to add is that returning a hash reference allows for beatiful syntax when you're using a single element from it.

    sub hash { qw(foo bar baz xyzzy) } sub hashref { { qw(foo bar baz xyzzy) } } my $dummy; # Using a single element only $dummy = ${ hash }{foo}; # UGLY! $dummy = hashref->{foo}; # Neat! # or hashref()->{foo} # or hashref->{'foo'} # etcetera.


    But in the end, it's all up to you. Be sure to document your choise!

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-18 02:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found