> On 03 Jul 2015, at 17:26, Tom Browder <[email protected]> wrote:
>
> 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;
> }
Whatever you like best of these two methods. A hash is an object, and as such
doesn’t get flattened when you pass it as a parameter to a sub (unlike Perl 5).
> # 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 \ creates a so-called Capture. Unless you really know what you’re doing, I
would say, don’t do that. Everything in Perl 6 is already an object that you
can pass along without fear of it getting flattened.
Liz