我在MooseX::Declare的BUILD
方法中遇到困難。如果我說:在MooseX :: Declare中創建BUILD方法的正確方法是什麼?
#!/usr/bin/perl
use MooseX::Declare;
class Foo {
has foo => (is => "rw", isa => "Str", default => "foo");
method BUILD {
print "I was called\n";
}
}
Foo->new;
我得到以下比有用的錯誤信息少:
Reference found where even-sized list expected at /Users/cowens/perl5/lib/perl5/MooseX/Method/Signatures/Meta/Method.pm line 335.
Validation failed for 'MooseX::Types::Structured::Tuple[MooseX::Types::Structured::Tuple[Object],MooseX::Types::Structured::Dict[]]' failed with value [ [ Foo=HASH(0x804b20) ], { HASH(0x8049e0) => undef } ], Internal Validation Error is: Validation failed for 'MooseX::Types::Structured::Dict[]' failed with value { HASH(0x8049e0) => undef } at /Users/cowens/perl5/lib/perl5/MooseX/Method/Signatures/Meta/Method.pm line 365
MooseX::Method::Signatures::Meta::Method::validate('MooseX::Method::Signatures::Meta::Method=HASH(0xb8aab0)', 'ARRAY(0xb8ab30)') called at /Users/cowens/perl5/lib/perl5/MooseX/Method/Signatures/Meta/Method.pm line 139
Foo::BUILD('Foo=HASH(0x804b20)', 'HASH(0x8049e0)') called at generated method (unknown origin) line 25
Foo::new('Foo') called at test.pl line 13
但如果我說:
#!/usr/bin/perl
use MooseX::Declare;
class Foo {
has foo => (is => "rw", isa => "Str", default => "foo");
sub BUILD {
my $self = shift;
print "I was called\n";
}
}
Foo->new;
一切工作得很好(但是是醜陋和出與其他代碼的地方)。
哇,這是醜陋的,`$ self`存在並保持祝福的對象,但你也有這個沒用hashref充滿了傳遞給`new`的參數。這是另一個美麗系統的重大瑕疵。 – 2009-07-21 05:04:39