http://qs321.pair.com?node_id=329956


in reply to Re: Re: Re: Re: Reducing Perl OO boilerplate
in thread Reducing Perl OO boilerplate

I've had to do exactly that with Class::MethodMaker. I needed to initialize an object with a LOT of data (and a LOT of method calls) and initializing the object through repeated method calls was S-L-O-W. So, I subverted the regular constructor with my own:
package Datawarehouse::Employee; use Class::MethodMaker new_with_init => 'new'; sub init { # ...Load up a whole lot of data # Bless my own object into the class Datawarehouse::Employee->new_new(\%args); } sub new_new { # This constructor is a HELL of alot faster than creating an # object by calling accessor methods one by one. my $class = shift; my $args = shift; my $obj = bless $args, $class; # Loading the whole hash as one }

If you do this while using the key_attrib parameter in Class::MethodMaker you'll need to call the key_attrib method explicitly so that your hand-made object is tracked like the regular one's are. Looking at the Class::MethodMaker source code is a great way of figuring out exactly what's going on with the autogenerated methods.

Gary Blackburn
Trained Killer