Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

How Do I pass a hash to a subroutine?

by filmo (Scribe)
on Feb 26, 2001 at 04:41 UTC ( [id://60798]=perlquestion: print w/replies, xml ) Need Help??

filmo has asked for the wisdom of the Perl Monks concerning the following question: (hashes)

How Do I pass a hash to a subroutine?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How Do I pass a hash to a subroutine?
by MeowChow (Vicar) on Feb 26, 2001 at 05:15 UTC
    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.
Re: How Do I pass a hash to a subroutine?
by Doraemon (Beadle) on May 11, 2004 at 13:43 UTC
    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);
Re: How Do I pass a hash to a subroutine?
by nite_man (Deacon) on Feb 13, 2003 at 12:22 UTC
    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; }

Re: How Do I pass a hash to a subroutine?
by thephpguy (Initiate) on May 06, 2011 at 18:06 UTC
    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.
Re: How Do I pass a hash to a subroutine?
by GoldenSage (Initiate) on Nov 28, 2012 at 21:41 UTC
    New here, maybe more of an explaination of prototypes? Tried to use your routine of (\%) which had syntax error at the line (also created syntax errors along every @_ line called in the library I'm writting. Looked at prototypes (first time I've seen such) at Prototypes. Hasn't made since yet, appears certain functions being called. Does look like a neat solution if it works for me.

    Originally posted as a Categorized Answer.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://60798]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-16 19:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found