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

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.