in reply to Problem generating builder functions with Moose for inherited objects

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:

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;
This way the code would be generated only once upon class creation, rather then instance creation.

-stvn
  • Comment on Re: Problem generating builder functions with Moose for inherited objects
  • Download Code

Replies are listed 'Best First'.
Re^2: Problem generating builder functions with Moose for inherited objects
by Neighbour (Friar) on Sep 15, 2010 at 10:06 UTC
    You are totally right. Thanks for pointing this silly mistake out :)