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

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.