
February 15th, 2001, 11:48 AM
|
|
Contributing User
|
|
Join Date: Aug 2000
Location: Indiana
Posts: 614
  
Time spent in forums: 4 h 49 m 49 sec
Reputation Power: 10
|
|
Code:
%hash = sub_name();
sub subname {
my %return;
#... do whatever here
return %return;
}
If you want to pass a hash to a sub routine (as well as other values), its best to pass the hash reference to the sub (that is, the memory allocation of the hash). I'll explain:
Code:
%hash_name = (name => 'value', name2 => 'value2');
%returned_hash = sub_hashpass(\%hash_name, <other values>);
sub sub_hashpass {
my $hash_ref = shift;
# Change a value of the hash
$hash_ref->{name} = value;
return %{$hash_ref};
}
This is just a basic example. You can do all sorts of cool stuff.
Note: if you change a value in $hash_ref [as I did in that sub routine] it will also change in the %hash_name hash. You can change that byref into a byval by point another hash to it's hash values:
my %new_hash = %{$hash_ref};
# Now changing values/keys in %new_has wont affect the original hash at all
I hope I helped.
|