Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

How Do I pass a hash to a subroutine?

by filmo (Scribe)
on Feb 26, 2001 at 04:41 UTC ( #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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

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

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (6)
    As of 2020-02-25 19:29 GMT
    Find Nodes?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?

      Results (113 votes). Check out past polls.