Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Passing hashes to subs?

by moxliukas (Curate)
on Aug 01, 2002 at 11:40 UTC ( #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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://186739]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2018-08-18 10:05 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (185 votes). Check out past polls.