in reply to Best practice with polymorphic constructors

I think you're trying to build too much functionality into the base class's constructor. You're also hard-coding a lot of stuff into this random config file, which means that any usage of these classes has to be with the config files. You wouldn't be able to just pull this class hierarchy and use it in a completely different application.

Why not do the following:

package Class::Parent; sub new { my $class = shift; my ($args) = @_; my $self; unless (ref $class) { $self = {}; bless $self, $class; } else { $self = $class; } my @keys = keys %$args; @{$self}{@keys} = @{$args}{@keys}; # At this point, you have $self blessed into the right # class and with the right keys. Specifically, whatever # is needed for the DBI. # Do DBI thing. return $self; } package Class::Child; use Class::Parent; @ISA = qw(Class::Parent); sub new { my $class = shift; my ($args) = @_; my $self = {}; bless $self, ref$class || $class; $self->SUPER::new($args); # Do whatever else Class::Child needs return $self; }
Thus, whenever you create a Foo::Database::Select, you're responsible for making sure it only goes and has select access. Of course, you could make sure that Foo::Database::Select only has a method to do selecting ...

Now, if you wanted to get crazier, you could have each child initialize itself instead of in the parent. I originally thought of doing that, but decided to do it the simpler way, in case that was complex enough. You might want to move the hashslice assignment to the child and within the unless side (for the parent).

------
We are the carpenters and bricklayers of the Information Age.

Vote paco for President!