Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-24 00:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found