为什么 Moo 的构建器方法只能访问其他一些属性?
Why does Moo's builder method have access to only some of the other attributes?
我期待构建器方法能够访问调用者提供的所有其他属性。但它似乎只能访问那些名称按字母顺序小于当前属性的名称。例如。为什么 b
的生成器在这里可以看到 a
的值,但看不到 c
的值? ('a' 和 'c' 都存在于最终对象中)
代码:
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
{
package P;
use Moo;
printf "Moo version: %s\n", $Moo::VERSION;
# a and c are defined in the same way
has a => ( is => 'ro' );
has c => ( is => 'ro' );
has b => (
is => 'ro',
builder => '_build_b',
);
sub _build_b {
my ($self) = @_;
print Data::Dumper->new(
[ $self ], [ 'self_during_build_b' ]
)->Indent(1)->Sortkeys(1)->Dump;
return "somebuiltvalue";
}
}
my $p = P->new({ a => 1, c => 3 });
print Data::Dumper->new([$p],['p'])->Indent(1)->Sortkeys(1)->Dump;
输出:
Moo version: 2.003004
$self_during_build_b = bless( {
'a' => 1
}, 'P' );
$p = bless( {
'a' => 1,
'b' => 'somebuiltvalue',
'c' => 3
}, 'P' );
我期待构建器方法能够访问调用者提供的所有其他属性。但它似乎只能访问那些名称按字母顺序小于当前属性的名称。例如。为什么 b
的生成器在这里可以看到 a
的值,但看不到 c
的值? ('a' 和 'c' 都存在于最终对象中)
代码:
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
{
package P;
use Moo;
printf "Moo version: %s\n", $Moo::VERSION;
# a and c are defined in the same way
has a => ( is => 'ro' );
has c => ( is => 'ro' );
has b => (
is => 'ro',
builder => '_build_b',
);
sub _build_b {
my ($self) = @_;
print Data::Dumper->new(
[ $self ], [ 'self_during_build_b' ]
)->Indent(1)->Sortkeys(1)->Dump;
return "somebuiltvalue";
}
}
my $p = P->new({ a => 1, c => 3 });
print Data::Dumper->new([$p],['p'])->Indent(1)->Sortkeys(1)->Dump;
输出:
Moo version: 2.003004
$self_during_build_b = bless( {
'a' => 1
}, 'P' );
$p = bless( {
'a' => 1,
'b' => 'somebuiltvalue',
'c' => 3
}, 'P' );