Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
more useful options
 
PerlMonks  

Re: Passing hashes as arguments to a sub?

by ikegami (Patriarch)
on Nov 28, 2005 at 12:54 UTC ( [id://512304]=note: 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.


in reply to Passing hashes as arguments to a sub?

Only scalars can be passed as arguments, so either need to pass a reference (like in the first and second snippet), or flatten the hash into a list of scalars (like in the third snippet).

One usually passes a reference to the hash which saves Perl from copying the entire hash:

sub function { my ($hash_ref) = @_; foreach (keys %$hash_ref) { ... } } function(\%hash);
If you don't with to work with references, you could use an alias:
sub function { our %hash; local *hash = \$_[0]; foreach (keys %hash) { ... } } function(\%hash);
Or you could create a copy of the hash if it's the last argument:
sub function { my %hash = @_; foreach (keys %hash) { ... } } function(%hash);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://512304]
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.