Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

( #149962=categorized answer: print w/ replies, xml ) Need Help??

Q&A > subroutines > Is it better to return a hash of hashes or a reference to a hash of hashes? - Answer 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.

  • Comment on Answer: Is it better to return a hash of hashes or a reference to a hash of hashes?
  • Download Code
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 studying the Monastery: (4)
As of 2016-07-01 04:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...











    Results (406 votes). Check out past polls.