Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Namespace weirdness with Parallel::ForkManager using 0 children

by Athanasius (Archbishop)
on Mar 21, 2016 at 07:32 UTC ( [id://1158414]=note: print w/replies, xml ) Need Help??


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

Adding the following line at the end of sub BUILD in “MyClass.pm” seems to fix the problem:

Role::Tiny->apply_roles_to_object($self, $module);

So my guess is: roles are, by default, class-based, rather than object-based, so that once a role has been added to a class, the definition of that class doesn’t change. But with multiple threads, the problem doesn’t arise as the class definition is re-created for each new thread.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^4: Namespace weirdness with Parallel::ForkManager using 0 children
by 1nickt (Canon) on Mar 21, 2016 at 08:14 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1158414]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-20 04:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found