I like to use references to the hash, but you can always just pass it to and from straight.
Perl will flatten your hash into a normal list. Luckily, if you're expecting this, you can capture it in a normal hash again.
my %values = (
name => 'chromatic',
rank => 'saint',
style => 'obnoxious',
);
my %rev_values = do_something(%values);
foreach my $key (keys %rev_values) {
print "$key => $rev_values{$key}\n";
}
sub do_something {
my %values = @_;
foreach my $key (keys %values) {
print "$key => $values{$key}\n";
$values{$key} = reverse $values{$key};
}
return %values;
}
A reference is conceptually more complicated, but it solves issues like parameter positioning and the trickiness involved in converting and copying hashes:
my %values = (
foo => 1,
bar => 2,
baz => 3,
);
my %rev_values = do_something(\%values);
sub do_something {
my $h_ref = shift;
my %backwards;
foreach my $key (keys %$h_ref) {
print "$key => ", $h_ref->{$key}, "\n";
$backwards{$h_ref->{$key}} = $key;
}
return \%backwards;
}
|