Also sprach Mark Healey:
I can't seem to find in in my perl book. I'd like to pass hashes to
functions
#!/usr/bin/perl -wT
my %someHash;
$someHash{'one'} = "1";
$someHash{'two'} = "2";
$someHash{'three'} = "3";
sub onsiestwosies
{
do stuff with %someHash
}
I'd simply like to pass %someHash to onesiestwosies.
Under well defined circumstances, you can simply do it like this:
sub func {
my %hash = @_;
}
...
func(%someHash);
The problem with that is that it a) copies the hash (which may be
inefficient if it is large) and b) you can't pass more than one list-ish
variable to the subroutine (perl flattens all arguments passed into one
large list). So best is to use a reference:
sub func {
my ($hash1_ref, $hash2_ref) = @_;
# do the dereferencing
my %hash1 = %$hash1_ref;
...
}
...
func(\%someHash1, \%someHash2);
See perlreftut.pod and perlref.pod if you aren't acquainted with
references. All about subroutines (including how to pass arguments and
access them), can be found in perlsub.pod.
Not sure whether you are familiar with the perldocs. You only mention a
book. The documentation of Perl is often more suitable for looking up such
things. See 'perldoc perl' for a list of available manpages and 'perldoc
perldoc' on how to access the Perl documentation.
Tassilo