I, like other responders, like to pass hash *references* as opposed to hashes. This comes in handy especially when you want to pass other things, as well as a hash, as arguments to a subroutine. Hashes and arrays get flattened out into one long string of scalar parameters, so if you stick to passing scalars (like hash references) around, you'll never have problems :-)

Here's a snippet from 'perldoc perlsub':
The Perl model for function call and return values is simple: a +ll func- tions are passed as parameters one single flat list of scalars, + and all functions likewise return to their caller one single flat list +of scalars. Any arrays or hashes in these call and return lists w +ill col- lapse, losing their identities--but you may always use pass-by- +refer- ence instead to avoid this. Both call and return lists may con +tain as many or as few scalar elements as you’d like. (Often a functio +n with- out an explicit return statement is called a subroutine, but th +ere’s really no difference from Perl’s perspective.)
If you instead pass parameters like this:
sub print_person { my $address_hashref = shift; my $hobbies_arrayref = shift; print "address line 1 = $address_hashref->{'addr1'}\n"; print "address line 2 = $address_hashref->{'addr2'}\n"; # etc, etc. # or, turn hashref back into a hash, and arrayref back into an +array my %address = %{$address_hashref}; my @hobbies = @{$hobbies_arrayref}; print "address line 1 = $address{'addr1'}\n"; } my %address = ( "addr1" => "1 Main St", "addr2" => "Suite 101", "city" => "Somecity", "state" => "Somestate" ); my @hobbies = ( "flying", "mountain climbing" ); print_person(\%address, \@hobbies);
HTH.

In reply to Re: passing subroutine args as a hash: why not? by hmerrill
in thread passing subroutine args as a hash: why not? by Willard B. Trophy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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, details, 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, summary, 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.