While experimenting I've found the first two methods of passing a hash
to a subroutine work:
# method 1
my %hash1;
foo1(%hash1);
say %hash1.perl;
sub foo1(%hash) {
%hash{1} = 0;
}
# method 2
my %hash2;
my $href2 = %hash2;
foo2($href2);
say %hash2.perl;
sub foo2($href) {
$href{1} = 0;
}
# this is what I naively tried first
# method 3 [DOESN'T WORK]
my %hash3;
my $href3 = \%hash3;
foo3($href3);
say %hash3.perl;
sub foo3($href) {
%($href}){1} = 0;
}
The perl dump shows the same results for the first two methods:
{"1" => 0}<>
So which is the preferred method (or more correct)?
Thanks.
Best regards,
-Tom