I'm having difficulty with attribute initialization order in the following:
driver.pl
my $foo = MyApp::Boom->new( configfile => "/home/todd/text.cfg" );
MyApp::Boom
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....
When I run the driver.pl, I sometimes get an error message "No Configuration File" and sometimes it works. I'm obviously missing something important about 'lazy' and 'required', but can't figure it out.
My understanding is the "configfile" would be set by the new call in driver.pl and the the attributes would initialize when called elsewhere and presumably 'configfile' would already be set.
Pointers, recommendations, or alternative approaches are welcome.