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!


In reply to Re: Best practice with polymorphic constructors by dragonchild
in thread Best practice with polymorphic constructors by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.