In short, don't create methods inside BUILD. You are making a common mistake of mixing up "object" and "class".
BUILD is called every time an instance is created, so every time you create an instance of Child or Parent, you will be overwriting all the builder methods, which no doubt is not what you want because it makes no sense.
All the code you have within BUILD could simply be put in the package declaration itself like so:
This way the code would be generated only once upon class creation, rather then instance creation.package Child; use Moose; extends 'Parent'; has 'knuisje' => ( is => 'rw', isa => 'Str', lazy_build => 1, ); { my $meta = __PACKAGE__->meta; foreach my $attribute ($meta->get_attribute_list) { $meta->add_method( '_build_' . $attribute, sub { my $self = shift; my $fropsel = $meta->get_attribute($attribute); if ($fropsel->type_constraint->name =~ /^Str/) { retur +n ""; } } ); } } 1;
In reply to Re: Problem generating builder functions with Moose for inherited objects
by stvn
in thread Problem generating builder functions with Moose for inherited objects
by Neighbour
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |