gpt4 book ai didi

perl - 在 MooseX::Declare 中创建 BUILD 方法的正确方法是什么?

转载 作者:行者123 更新时间:2023-12-04 17:42:13 26 4
gpt4 key购买 nike

我在使用 BUILD 时遇到困难MooseX::Declare 中的方法.如果我说:

#!/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;

一切正常(但丑陋且与其余代码格格不入)。

最佳答案

BUILD 需要一个 arg,如果你不需要它,就说:

method BUILD($) { ... }

关于perl - 在 MooseX::Declare 中创建 BUILD 方法的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1155814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com