I'm sure I've asked this in the cb or on #perl before, but I don't remember the suggestions given to me, so I'll ask it here for posterity.

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...

package Thingy; sub get_cog { my ($self) = @_; return Thingy::Cog->new; }
and I want to make it extensible when Thingy is subclassed (and Thingy::Cog is potentially subclassed). I could do
sub get_cog { my (@self) = @_; return( (ref($self) . "::Cog")->new ); }
but that breaks if the person subclassing Thingy has not also subclassed Thingy::Cog.

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

In reply to Inheritance in a Suite of Modules by japhy

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.