Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

( #117470=categorized question: print w/ replies, xml ) Need Help??
Contributed by uwevoelker on Oct 08, 2001 at 19:15 UTC
Q&A  > subroutines


Answer: Is it better to return a hash of hashes or a reference to a hash of hashes?
contributed by merlyn

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...

Answer: Is it better to return a hash of hashes or a reference to a hash of hashes?
contributed by Aristotle

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.

Answer: Is it better to return a hash of hashes or a reference to a hash of hashes?
contributed by Russ

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.

Answer: Is it better to return a hash of hashes or a reference to a hash of hashes?
contributed by Juerd

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.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (12)
    As of 2014-09-19 11:37 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (136 votes), past polls