in reply to Re^3: Converting Moose object to a subclass of itself
in thread Converting Moose object to a subclass of itself
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Converting Moose object to a subclass of itself
by nysus (Parson) on Feb 26, 2017 at 23:42 UTC | |
by stevieb (Canon) on Feb 27, 2017 at 00:08 UTC | |
by nysus (Parson) on Feb 27, 2017 at 01:05 UTC | |
by stevieb (Canon) on Feb 27, 2017 at 01:19 UTC | |
by nysus (Parson) on Feb 27, 2017 at 01:39 UTC | |
by nysus (Parson) on Feb 27, 2017 at 01:36 UTC | |
|