sub function
{
 my %operating_systems = &populate_hash(%operating_systems);
 do_something_with_hash();
}

sub populate_hash
{
 my %operating_systems = ?????;
 $operating_systems{"microsoft"} = "Windows";
 $operating_systems{"apple"} = "Macintosh";
 ...
 return %operating_systems;
}


What should the ????? be? If it was an array, it would be @_; if it
was a scalar, it would be
my ($scalar) = @_, $_[0], or shift; How about with a hash?


Generally you would pass hash's reference to a subroutine,and the same return hash's reference from the subroutine.
The example codes look like:

sub function {
   my %hash = ('a' => 1, 'b' => 2);
   # here pass  \%hash (a reference to hash) as parameter to the subroutine
   my $hash_ref = populate_hash(\%hash);
}

sub  populate_hash {
   # here accept the hash's reference as parameter from the caller
   my $hash_ref = shift;
   # access and modify the hash via its reference
   $hash_ref->{'a'} = 3;
   # return the reference
   return $hash_ref;
}

Hope it help.



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to