Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Re: Re: Re: Reducing Perl OO boilerplate

by flyingmoose (Priest)
on Feb 18, 2004 at 14:55 UTC ( #329923=note: print w/replies, xml ) Need Help??

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

Class::MethodMaker appears to be exactly what I want in terms of defining data. Good find. I need to still do some exploring to find what I can do in terms of constructor cleanup (so that I don't need to call object methods to set all of the variables after 'new' is invoked .. aka constructors that take parameters like they are supposed to), but this will help with method cleanup quite a bit. Thanks!
  • Comment on Re: Re: Re: Re: Reducing Perl OO boilerplate

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Reducing Perl OO boilerplate
by Trimbach (Curate) on Feb 18, 2004 at 16:45 UTC
    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

      Just combing through the POD, I ran across this.

      new_hash_init Creates a basic constructor which accepts a hash of slot-name/value pairs with which to initialize the object. The slot-names are interpreted as the names of methods that can be called on the object after it is created and the values are the arguments to be passed to those methods. Takes a single string or a reference to an array of strings as its argument. For each string creates a method of the form listed below. Note that this method can be called on an existing objec, which allows it to be combined with new_with_init (see above) to provide some default values. (Basically, declare a new_with_init method, say 'new' and a new_hash_init method, for example, 'hash_init' and then in the init method, you can call modify or add to the %args hash and then call hash_init.)
      sub <string> { my ($class, %args) = @_; my $self = {}; bless $self, $class; foreach (keys %args) { $self->$_($args{$_}); } $self; }

      Sounds like new_with_init except with my name/value pairs request taken into account. Excellent tip. Rock on!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2020-07-02 06:14 GMT
Find Nodes?
    Voting Booth?

    No recent polls found