I think I see what you're getting at. Personally, I'd write the constructor like so:
package Base_class; ## new (args:hashref) : Object_ref # # create a new object, then initialize it from class-specific # defaults and/or argument values. # sub new { my $O = bless {}, shift; my $args = shift; my $defaults = $O->_defaults; ## iterate over the keys in %$defaults, because those are all ## we really care about: for $k (keys %$defaults) { $O->{ $k } = (defined $args->{$k}) ? $args->{ $k } : $defaults->{ $k }; } return ($O); } package Subclass_1; @ISA = qw( Base_class ); ## _defaults (nil) : hashref # # return a hash of default values for objects of this class # sub _defaults { ## this is basically a static variable for the class. i ## define such things at runtime, but that's just a personal ## taste. if ( ! defined $DEFAULTS ) { $DEFAULTS = { 'attr1' => 'val1', 'attr2' => 'val2', 'attr3' => 'val3', }; } return ($DEFAULTS); }
That gives you the freedom to create as many subclasses as you want, each with its own signature of attributes. Each subclass will only accept values from its own signature, and every object will be fully initialized to its own signature by the time the ref leaves new().
In reply to Re: Re: Re: OO Perl and Design Issues
by mstone
in thread OO Perl and Design Issues
by jonjacobmoon
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |