如何正确完成?
> sub adding(:$first, :$second) { $first + $second }
> adding(second => 2, first => 1)
3
> my %param = second => 2, first => 1
{first => 1, second => 2}
> adding(%param)
Too many positionals passed; expected 0 arguments but got 1
in sub adding at <unknown file> line 1
in block <unit> at <unknown file> line 1
答案 0 :(得分:12)
向|
%param
adding( |%param )
这也适用于位置参数
sub foo ( $bar, $baz ){ "$bar:$baz" }
my @args = 'a', 'b';
foo( @args ) # error
foo( |@args ) # "a:b"