Reputation: 5318
I'd like to have an immutable objects with modifiable fields, so that $foo->bar()
returns the bar
of foo
, and $foo->bar(42)
returns a new object with bar
set to 42, leaving $foo
intact.
Of course, I can do that with Moo[se]:
package Foo;
use Moo;
has bar => is => 'lazy', builder => sub { 0 };
around bar => sub {
my ($orig, $self, $set) = @_;
if (@_ < 3) {
return $orig->($self);
} else {
return (ref $self)->new( %$self, bar => $set );
}
};
But this is a bit tedious, so is there a module to add accessors with such signatures?
Upvotes: 3
Views: 88