如果我使用MooseX::Method::Signatures
,并且我想将所有参数传递给第二个方法,我必须再次明确地列出它们:
method foo (Str :$bar!, Int: :$baz!) {
...
return $self->_foo2(bar => $bar, baz => $baz);
}
如果我可以做的事情会很好:
method foo (Str :$bar!, Int: :$baz!) {
...
return $self->_foo2(%args);
}
Method::Signatures
的此文档建议我可以使用@_
,但会丢弃命名参数键。
答案 0 :(得分:2)
做了一点测试后,似乎MooseX :: Method :: Signatures是"奇怪的一个"方法签名的主要实现方式。所有其他人允许您按预期使用@_
; MXMS没有。
use strict;
use warnings;
use Test::More 0.96;
{
package MyBase;
sub new { bless {}, shift }
sub _foo { \@_ }
}
{
package UseKavorka;
use Kavorka;
use parent -norequire => qw(MyBase);
method foo (Str :$bar!, Int :$baz!) {
$self->_foo(@_);
}
}
{
package UseMS;
use Method::Signatures;
use parent -norequire => qw(MyBase);
method foo (Str :$bar!, Int :$baz!) {
$self->_foo(@_);
}
}
{
package UseMXMS;
use Moose;
use MooseX::Method::Signatures;
extends qw(MyBase);
method foo (Str :$bar!, Int :$baz!) {
$self->_foo(@_);
}
}
{
package UseFP;
use Function::Parameters;
use parent -norequire => qw(MyBase);
method foo (Str :$bar, Int :$baz) {
$self->_foo(@_);
}
}
for my $class (qw/ UseKavorka UseMS UseMXMS UseFP /)
{
my $obj = $class->new;
is_deeply(
$obj->foo(bar => "Hello world", baz => 42),
[ $obj, bar => "Hello world", baz => 42 ],
"\@_ worked fine in $class",
);
}
done_testing;
__END__
ok 1 - @_ worked fine in UseKavorka
ok 2 - @_ worked fine in UseMS
not ok 3 - @_ worked fine in UseMXMS
# Failed test '@_ worked fine in UseMXMS'
# at foo.pl line 55.
# Structures begin differing at:
# $got->[1] = UseMXMS=HASH(0x92c0cc8)
# $expected->[1] = 'bar'
ok 4 - @_ worked fine in UseFP
1..4
# Looks like you failed 1 test of 4.
我有偏见是因为我写了它,但我的建议是切换到Kavoka,它提供了几乎MooseX :: Method :: Signatures的所有功能,但没有大幅减速。