我在下面的属性初始化顺序方面遇到了困难:
driver.pl
my $foo = MyApp::Boom->new( configfile => "/home/todd/text.cfg" );
MyApp的::臂架
has configfile => (
is => 'ro', isa => 'Str', required => 1
);
has conf => (
is => 'ro', isa => 'HashRef', required => 1, lazy => 1,
builder => '_build_configuration'
);
sub _build_configuration {
my $self = shift;
unless ( $self->configfile ) {
die "No Configuration File!";
}
# stuff that reads the file and creates a hashref
return $href;
}
has baz => (
is => 'ro', isa => 'MyApp::Baz', required => 1, lazy => 1,
builder => '_build_baz',
);
sub _build_baz {
my $self = shift;
my $conf = $self->conf;
# uses data in conf to do stuff and create a MyApp::Baz
return MyApp::Baz->new($conf);
}
# other stuff omitted....
当我运行driver.pl时,我有时会收到一条错误消息" No Configuration File"有时候它有效。我显然遗漏了一些重要的关于“懒惰”的事情。并且'要求',但无法弄清楚。
我的理解是" configfile"将由driver.pl中的新调用设置,并且属性将在其他地方调用时初始化,并且可能是' configfile'已经设定好了。
欢迎使用指针,建议或替代方法。
答案 0 :(得分:0)
感谢大家的帮助。我通过调试器逐行运行实际代码,发现有一个属性埋没在没有lazy => 1的子类中。