Subject: | Moose does not initialize attributes passed to the constructor before others |
This can lead to some confusing situations if an attribute which is not explicitly set to lazy has a default or builder which relies on an attribute which has been set via the constructor. For example:
#!/usr/bin/perl
package Foo;
use Moose;
has set_me => (
is => 'ro',
isa => 'Str',
required => 1,
);
has read_me => (
is => 'ro',
isa => 'Str',
default => sub {
my $self = shift;
$self->was_set( $self->set_me );
return $self->set_me;
},
#lazy => 1,
);
has was_set => (
is => 'rw',
isa => 'Str',
);
1;
package main;
use strict;
use warnings;
use feature qw( say );
my $foo = Foo->new( set_me => 'donuts' );
say $foo->set_me;
say $foo->read_me;
say $foo->was_set;
For the above script, as long as the lazy is commented out, this script will randomly execute to completion or die, depending on the order of the attribute initialization, which is also random.
If this is something which is fixable, I do have some $work time set aside to address it. I'd just need some pointers on how to go about it.
Thanks,
Olaf