扩展 Moose 对象类的构造顺序是什么?
我编写了一个 Moose 对象类,它扩展了另一个 Moose 对象类:
package MySubClass;
use Moose;
extends MySuperClass;
我有一个属性,我想在对象实例化的基础上自动构建该属性:
has 'id' => (
is => 'ro',
isa => 'Str',
builder => '_build_id',
init_arg => undef,
);
sub _build_id {
my $self = shift;
# both ssn and bnn are attributes of MySuperClass
return $self->ssn . $self->bnn;
}
除非我将 id 设置为惰性,否则这不起作用。为什么?
扩展对象的构造是如何完成的?
I wrote a Moose object class which extends another Moose object class:
package MySubClass;
use Moose;
extends MySuperClass;
I have an attribute which I'd like to automatically build upon object instantiation:
has 'id' => (
is => 'ro',
isa => 'Str',
builder => '_build_id',
init_arg => undef,
);
sub _build_id {
my $self = shift;
# both ssn and bnn are attributes of MySuperClass
return $self->ssn . $self->bnn;
}
This doesn't work unless I set id
as lazy. Why?
How is the construction of extend
ing objects done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
引用 Moose::Manual::Laziness 属性:
Quote Moose::Manual::Attributes on Laziness: