Consider this program where I create a hash. I want to then change two values in it:
my $hash = %(
wallet => 100,
gave => 0,
received => 0,
);
for ^1 { $hash<wallet gave> Z+= <-1 1> };
dd $hash;
Like this, the last line of for
doesn't do anything and there is no warning. The hash is unchanged:
Hash $hash = ${:gave(0), :received(0), :wallet(100)}
Adding another statement changes the behavior:
my $hash = %(
wallet => 100,
gave => 0,
received => 0,
);
for ^1 { $hash<wallet gave> Z+= <-1 1>; True };
dd $hash;
Now the inplace edit does its thing, but there's a warning (although I dispute "useless" when I've found a use for it):
Useless use of constant value True in sink context
Hash $hash = ${:gave(1), :received(0), :wallet(99)}
If I do without the Z+=
, which should be the same thing, it works:
my $hash = %(
wallet => 100,
gave => 0,
received => 0,
);
for ^1 { $hash<wallet gave> = $hash<wallet gave> Z+ <-1 1> }
dd $hash;
Again the right output:
Hash $hash = ${:gave(1), :received(0), :wallet(99)}
It's a bug. Fixed as of Rakudo 2018.02.1-45-g8a10fc1
来源:https://stackoverflow.com/questions/45001820/which-context-confuses-this-perl-6-zip-operator