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


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

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!