japhy has asked for the wisdom of the Perl Monks concerning the following question:
I have a suite of classes (Thingy, Thingy::Cog, Thingy::Handle, Thingy::Ramp, etc.) that can be inherited from so that their methods can be overridden. Thing is (no pun intended), Thingy objects need to generate Thingy::Cog objects. Therefore, when a class derives from Thingy (such as MyThingy) it should auto-generate inheriting classes with the same prefix (autovivifying MyThingy::Cog, MyThingy::Handle, etc.).
I'd rather not hard-code more than I have to in this case, unless a purely dynamic solution is disgusting to look at and code.
Update: I apologize if I was unclear. Here's a better explanation. A Thingy object produces a Thingy::Cog object. If I sub-class Thingy as MyThingy, I want the MyThingy object to attempt to create a MyThingy::Cog object instead of the hardcoded Thingy::Cog object. If MyThingy::Cog doesn't exist, then Thingy::Cog should be the class used.
What this means is that I have this code...
and I want to make it extensible when Thingy is subclassed (and Thingy::Cog is potentially subclassed). I could dopackage Thingy; sub get_cog { my ($self) = @_; return Thingy::Cog->new; }
but that breaks if the person subclassing Thingy has not also subclassed Thingy::Cog.sub get_cog { my (@self) = @_; return( (ref($self) . "::Cog")->new ); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Inheritence in a Suite of Modules
by jdporter (Paladin) on Mar 27, 2006 at 22:37 UTC | |
|
Re: Inheritence in a Suite of Modules
by ikegami (Patriarch) on Mar 27, 2006 at 21:30 UTC | |
|
Re: Inheritence in a Suite of Modules
by xdg (Monsignor) on Mar 27, 2006 at 22:16 UTC | |
|
Re: Inheritence in a Suite of Modules
by roboticus (Chancellor) on Mar 27, 2006 at 21:09 UTC |