It looks like you've come with a workable solution, but not a particularly flexible one. What happens if you extend Utils with Utils::MidLevel?

As you clearly understand, no software is smart enough to know that if you typed new Utils() you really meant new Utils::LowLevel(), but from a software design standpoint there's no reason you couldn't have a single new() function that handled instantiation for Utils, Utils::MidLevel, and Utils::LowLevel.

The way to do that, if I remember correctly, is to recall that you can bless an object into almost any class you want. So, where you normally see bless $self, $class, there's no reason not to interpolate $class to be any of the three classes you've designed.

Here's where you can get tricky... what if each of these classes had an init() function that would perform class-specific stuff. Your code could become something like this:

package Utils; sub new { my $class = shift; my $self = {}; # Bless into the appropriate class my $obj = bless $self, $class; $obj->init(@_); return $obj; } sub init { # does nothing } package Utils::MidLevel @Utils::MidLevel::ISA = qw(Utils); sub init { my $self = shift; my @args = @_; $self->SUPER::init(@args); # Now do initialization of object } package Utils::LowLevel @Utils::LowLevel::ISA = qw(Utils::MidLevel); sub init { my $self = shift; my @args = @_; $self->SUPER::init(@args); # Now do initialization of this class }

Your initialization could then be:

my $object = new Utils::LowLevel();

But this would also work:

my $object = new Utils();

I guess this is kind of tangential to your question, because what you're doing is the only way of doing what you appear to want to do (save yourself from test failures that are the result of typos). However, I'd strongly suggest that this is not a good coding practice for the reason I outlined above (to put it another way, if a Util is always a LowLevel Util then I'm a little unclear on why you'd have a separate class at all).

HTH

PS. As with all things Perl there are other ways to do this, but the init() system is fairly simple to comprehend and gives you a fair amount of flexibility.


In reply to Re: Faking new() method in high-level package by jreades
in thread Faking new() method in high-level package by belden

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.