On Sun, 3 Jun 2001, Nic LAWRENCE wrote:
> Is it possible to pass hashes between subroutines? When I try stuff like
> &foo(%bar); or return %bar; it doesn't seem to work how I would expect.
> :-/
You need to pass by reference. Read perlref for details, but try this.
Use "&foo(\%bar)" or "return \%bar" to pass the reference. Note the \.
That turns an object into a reference.
A reference is contained in a $scalar. To de-reference it, you need
to put the approprite symbol in front. So the foo routine would be...
sub foo {
my %hash = %{$_[0]};
$hash{whatever} = ...
print "Hi, $hash{name}\n";
}
Make sure you return MYed hashes from subroutines, so each call
gets a new hash and doesn't re-use old ones filled with data.
--
Ian