Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Passing hashes to subs?

by moxliukas (Curate)
on Aug 01, 2002 at 11:40 UTC ( [id://186739]=note: print w/replies, xml ) Need Help??


in reply to Passing hashes to subs?

You can pass the entire hash as a hash reference. This would also make things more efficient. Here is the sample code:

my %hash_outside; $hash_outside{'first'} = 'first value'; $hash_outside{'second'} = 'second value'; sub blah { my %hash = %{ +shift; }; foreach my $k (keys %hash) { print "$k -> " . $hash{$k} . "\n"; } } &blah(\%hash_outside);

Here you pass a hash reference to the sub by appending a backslash ( \%hash_outside ). First line in the sub dereferences the reference to the hash so you can use it as a normal hash.

You could also look at perlref documentation to find out more about references.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-18 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found