Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

How Do I pass a hash to a subroutine?

( #60798=categorized question: print w/ replies, xml ) Need Help??
Contributed by filmo on Feb 26, 2001 at 04:41 UTC
Q&A  > hashes


Answer: How Do I pass a hash to a subroutine?
contributed by MeowChow

Hashes can be passed either by value:

mysub(%hash); sub mysub { my (%params) = @_; }
or by reference:
mysub(\%hash); sub mysub { my $params = shift; my %paramhash = %$params; }
Each approach has it's advantages and disadvantages. Pass-by-value offers a simpler syntax, while pass by reference is faster and more flexible, allowing you to easily intermingle the hash with other parameters.
Answer: How Do I pass a hash to a subroutine?
contributed by Doraemon

And, if you need to pass multiple parameters (eg. a hash and then an array) pass by reference; otherwise the hashes and arrays are flattened into a single list and the sub can't tell what is part of which.

Here is an example of using prototypes to force a pass-by-reference.

sub foo(\%\@) { my ($hash_ref, $array_ref) = @_; #... } foo (%hash, @array);
Answer: How Do I pass a hash to a subroutine?
contributed by nite_man

Also, you can use prototypes to get special argument passing:

my_sub(%hash) sub my_sub(\%) { my $hash_ref = shift; map { print "$_ => $$hash_ref{$_} \n" } keys %$hash_ref; }

Answer: How Do I pass a hash to a subroutine?
contributed by thephpguy

MeowChow's code for passing by reference is VERY inefficient unless you actually want a copy of the Hash. (Why pass by ref if you want a copy?)

mysub(\%hash); sub mysub { my $paramhash = $_[0]; }
With the above code any modification will affect the original hash.

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 lurking in the Monastery: (15)
    As of 2014-10-23 16:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (125 votes), past polls