![]() |
|
Come for the quick hacks, stay for the epiphanies. | |
PerlMonks |
Re: [Moose] extends(...) throws "Subroutine redefined" warningsby kcott (Archbishop) |
on Oct 08, 2015 at 04:58 UTC ( [id://1144139]=note: print w/replies, xml ) | Need Help?? |
G'day muba, While I appreciate you've only shown a minimal example, and there could be a lot more going on than I'm aware of, my gut feeling is that you have a design flaw here. I say that because parent classes shouldn't know, or care, about their subclasses. If I change your test one-liner to something a little more substantial, I get much the same as ++Athanasius did in his response:
By swapping the order in which the two modules are loaded in MyClass.pm, i.e.
your problem disappears:
However, I make absolutely no guarantees that this will not cause other problems with your complete code (which I haven't seen). If I remove the use SubClass; from MyClass.pm, and load SubClass.pm, instead of MyClass.pm, in the test:
the compile-time error is eradicated. I have a superclass (MyClass), which creates instances of a subclass on demand." Change &get_subclass_object to be more generalised. Something like:
Create another subclass (OtherSubClass.pm) for testing:
Now you can instantiate whatever subclasses you want:
You can create as many subclasses as you want. MyClass knows nothing about them. All subclasses use the same method: &MyClass::get_subclass_object. The design flaw is now eradicated. The SYNOPSIS of the base Moose documentation has an example showing the use of extends. Read on for more information on extends in that documentation (which has links to further details). — Ken
In Section
Seekers of Perl Wisdom
|
|