I believe I understand. What I'd do, is have your Parent have methods that returns the appropriate thing:

# parent sub new { return bless {}, shift; } sub wordpress { my ($self, %params) = @_; return My::Dist::Wordpress->new(..., %params); } # user code my $parent = My::Dist->new; my $wordpress_obj = $parent->wordpress(%params);

Does that make sense? Again, you can hold the child objects within the parent object if you need to maintain running information about them. Let me know if you'd like a brief example of that scenario.

Note that some parent class code sets up a lot of stuff before returning a child object. Other times not. If not, you could simply do:

my $wp_obj = My::Dist::Wordpress->new(...);

That bypasses parent->child inheritance, so fetching from the parent obj may be better, again, because you may want to keep track of child info, or you may want all of your DB/config/setup stuff within the parent, then have the parent create the child with a special instantiation call, while passing in this special data. Doing it through the parent object (former example) would have parent do *all* of the config reading, then just pass along relevant details to the relevant child init (this is my preferred way in most situations, so configs etc get read only once).

When I create multi-level inheritance classes, iirc, I 'hide' all of the child classes, so that a user doesn't need to know about them whatsoever. See here for a pretty simple example where I let the parent pull in objects of different types, and dish them out. In that case, there's no state tracking.

Here's a reasonably more complex example, where I have two classes, parent and child, where I stuff the child inside the parent so the parent can use the child directly, and the child can actually use the parent as well. This requires some REFCNT (reference counting) manipulation, so that we don't leak.


In reply to Re^4: Converting Moose object to a subclass of itself by stevieb
in thread Converting Moose object to a subclass of itself by nysus

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.