Ha, that's actually what I suspected, that the package was being permanently altered in Perl's brain the first time through. I just didn't think that with() operated on the package level, as does apply_roles_to_package (which I was eschewing because of that).

But it gets back to what I mentioned in an earlier discussion, which is that using apply_roles_to_object seems to break the whole point of Roles, in that using it in sub BUILD in the parent causes the child's method to be loaded *before* the parent's of the same name. Notably, placing the conditional code inside an after BUILD => sub { ... }; block makes no difference to the outcome.

package MyClass; use Moo; use Moo::Role(); sub BUILD { my ( $self, $args ) = @_; my $module = $args->{'num'} % 2 ? 'Odd' : 'Even'; warn "$args->{'num'} $self loading $module"; Moo::Role->apply_roles_to_object( $self, $module); return $self; }; sub baz { return 'qux'; } 1;
And in each of the Roles:
sub baz { die "Died in ", __PACKAGE__; }
Output:
1 MyClass=HASH(0x19c12c0) loading Odd at MyClass.pm line 14. Loaded Odd at Odd.pm line 4. 1 MyClass__WITH__Odd=HASH(0x19c12c0)->bar at test.pl line 11. Died in Odd at Odd.pm line 11.

I guess I will just code around that; indeed I don't reuse method names anywhere as a practise ... but it seems odd. Oh well. ++Athanasius for helping me figure out that I was just seeing another symptom of the same issue I was dealing with earlier!


The way forward always starts with a minimal test.

In reply to Re^4: Namespace weirdness with Parallel::ForkManager using 0 children by 1nickt
in thread SOLVED [ create_class_with_roles() ]: Moo[se] namespace weirdness with conditionally composed-in Roles by 1nickt

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.