Reset default attribute

I can declare an attribute with Moose as follows:

has 'attr' => (is => 'rw', isa => 'Int', default => 10); 

Is reset possible for this default value?

Example:

 $obj->attr(5); # sets attr to 5 $obj->_reset_attr; print $obj->attr; # will print 10 
+4
source share
1 answer

If you do this:

 has 'attr' => ( is => 'rw', isa => 'Int', lazy => 1, default => 10, clearer => '_clear_attr', ); 

then you can do:

 my $obj = Class->new; print $obj->attr; # 10 $obj->attr(5); print $obj->attr; # 5 $obj->_clear_attr; print $obj->attr; # 10 

The combination of lazy and clearer is important here.

+6
source

Source: https://habr.com/ru/post/1482630/


All Articles