in reply to RE: RE: RE: Re: Factory Pattern for Class Heirarchy
in thread Factory Pattern for Class Heirarchy

if ($objectName->can('new')) { return $objectName->new(@_); # Pass in any remaining a +rgs } else { die "'$objectName' does not contain a constructor."; };
This is both overkill and inaccurate. A constructor does not have to be named new, so the die message is inaccurate. And it's going to die anyway if you can't find the method, so just use:
return $objectName->new(@_); # I hope they know how!

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
RE: RE: RE: RE: RE: Re: Factory Pattern for Class Heirarchy
by johannz (Hermit) on Oct 10, 2000 at 02:43 UTC

    I realize that the constructor does not have to be named 'new' but I was going with the generalization (which I wasn't explicit about) that they would be using the same constructor as the base class. On further thought, since this test should always succeed if they were inheriting from the base class, your correction is better. My code really really doesn't test what I thought it was going to. He might consider checking $objectName->isa('My::Baseclass') to at least verify that at least the new class is inherited from the base class that he hopes it is. This is just to make debugging easier by catching errors sooner rather than later.

    Remember: This was a quick and dirty hack to try and get an basic answer for the question.