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:

$ perl -MMyClass -le 'print SubClass::->new()->get_subclass_object()' MyClass already has a metaclass, but it does not inherit Moose::Meta:: +Class (Class::MOP::Class=HASH(0x7f918c72e440)). at /Users/ken/perl5/p +erlbrew/perls/perl-5.22.0t/lib/site_perl/5.22.0/darwin-thread-multi-2 +level/Moose/Exporter.pm line 484 Moose::import('Moose') called at MyClass.pm line 3 MyClass::BEGIN at MyClass.pm line 3 eval {...} at MyClass.pm line 3 require MyClass.pm at -e line 0 main::BEGIN at MyClass.pm line 3 eval {...} at MyClass.pm line 3 BEGIN failed--compilation aborted at MyClass.pm line 3. Compilation failed in require. BEGIN failed--compilation aborted.

By swapping the order in which the two modules are loaded in MyClass.pm, i.e.

package MyClass; use Moose; use SubClass;

your problem disappears:

$ perl -MMyClass -le 'print SubClass::->new()->get_subclass_object()' SubClass=HASH(0x7fc4f2bb4148)

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:

$ perl -MSubClass -le 'print SubClass::->new()->get_subclass_object()' SubClass=HASH(0x7fb6698259a8)

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:

sub get_subclass_object { my $class = ref $_[0]; return $class->new; }

Create another subclass (OtherSubClass.pm) for testing:

package OtherSubClass; use Moose; extends 'MyClass'; 1;

Now you can instantiate whatever subclasses you want:

$ perl -MSubClass -le 'print SubClass::->new()->get_subclass_object()' SubClass=HASH(0x7fae2280f258)
$ perl -MOtherSubClass -le 'print OtherSubClass::->new()->get_subclass +_object()' OtherSubClass=HASH(0x7fa721030da8)

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 reply to Re: [Moose] extends(...) throws "Subroutine redefined" warnings by kcott
in thread [Moose] extends(...) throws "Subroutine redefined" warnings by muba

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.