Can I make the Moose attribute “write once”?

I would like to have an optional Moose attribute that can only be set once.

If I use is => 'ro' , I have to set the attribute when creating the object, but I want it to be possible to add after it (while it is not set yet).

+6
perl moose
source share
2 answers
+12
source share

Use the method modifier:

 has 'attr' => ( is => 'rw', predicate => 'is_set', ... }; before 'attr' => sub { my $self = shift; die 'attr already set' if $self->is_set; }; 
+5
source share

All Articles