Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Passing hashes to subs?

by flocto (Pilgrim)
on Aug 01, 2002 at 11:41 UTC ( [id://186740]=note: print w/replies, xml ) Need Help??


in reply to Passing hashes to subs?

Besides the options mentioned above (using references..) you can also do either of these:

foo (%hash); sub foo { my %passed_hash = @_; # do stuff }

...which only workes if the hash is the only argument you have (or the last one..). If it is not, or you have more than one hash to pass, and your subroutine is seen at sompile ime, you can use this elegant solution:

sub foo (\%$); foo (%hash, $arg); sub foo (\%$) { my $hash_ref = shift; my $passed_arg = shift; my $passed_hash = %$hash_ref; # do stuff }

The second example provides a prototype for the subroutine. How these work can be read in perlsub.. But to give you some clue: It automatically converts the hash into a reference..

Regards,
-octo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-03-28 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found