|Problems? Is your data what you think it is?|
Re: Faking new() method in high-level packageby jreades (Friar)
|on May 08, 2002 at 21:54 UTC||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:
Your initialization could then be:
But this would also work:
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).
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.