Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Just another Perl shrine
 
PerlMonks  

How Do I pass a hash to a subroutine?

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

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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 00: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 nite_man (Deacon) on Feb 13, 2003 at 07: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 Doraemon (Beadle) on May 11, 2004 at 09: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 thephpguy (Initiate) on May 06, 2011 at 14: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 16: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.

Re: How Do I pass a hash to a subroutine?
by GoldenSage (Initiate) on Nov 28, 2012 at 17:06 UTC
    ack.. ignore my previous post please, typo.. adn no edit button, however doesn't seem to have any values in the hash passed.
    
    &formtable(%FORM);
    
    sub formtable(\%) {
        my $hash_ref = shift;
        my %hash = %$hash_ref;
        foreach $key (%hash){
            print "$key=$hash{$key}";
        }
    }# Close of formtable
    
    
    prints nothing and I test the %FORM with same foreach loop and values print... ? globals? Have to use $_[0]??? etc.
      Calling a subroutine with & ignores prototypes. Also, in order to make prototypes work, you must declare the subroutine before calling it.

      Moreover, you are posting to the Answers section, while you probably wanted to reply to one of the answers.

      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://60798]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.