Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Problem generating builder functions with Moose for inherited objects

by stvn (Monsignor)
on Sep 14, 2010 at 18:26 UTC ( #860130=note: print w/ replies, xml ) Need Help??


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
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 :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://860130]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2014-07-30 08:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls