I would like to have a non-required Moose attribute that can be set only once.
If I use is => 'ro'
I must set the attribute upon creation of the object, but I want to be able to add it afterwards (as long as it's not been set already).
MooseX::SetOnce
Use a method modifier:
has 'attr' => (
is => 'rw',
predicate => 'is_set',
...
};
before 'attr' => sub {
my $self = shift;
die 'attr already set' if $self->is_set;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With