in reply to Re^4: Is there a better way to do this?
in thread Is there a better way to do this?

Hi choroba,

That doesn't work for me. I had tried that because I knew that Moo::Role is based on Role::Tiny, but got an error:

package MyClass; use Moo; #use Role::Tiny(); sub BUILD { my $self = shift; # Role::Tiny->apply_roles_to_package( __PACKAGE__, 'MyClass::Child' + ); 'Moo::Role'->apply_roles_to_package( __PACKAGE__, 'MyClass::Child' + ); return $self; }; sub foo { return 'foo'; } 1;
Output:
Can't locate object method "apply_roles_to_package" via package "Moo:: +Role" at MyClass.pm line 10.

The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^6: Is there a better way to do this?
by choroba (Cardinal) on Mar 19, 2016 at 18:45 UTC
    Have you loaded Moo::Role anywhere? I can reproduce the issue if I don't load it. Replacing line 4 with

    use Moo::Role ();

    should fix it without issues with redefined subroutines.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Yeah, that does the trick. I was misled because when you try to use Moo::Role; with use Moo;, it doesn't report the redefined subs error, but just says Cannot import Moo::Role into a Moo class.

      So then it's basically just the same as use Role::Tiny() I guess ... maybe a slightly larger footprint, in fact. In my real application I don't use has() in the children, so can just use Role::Tiny; there, with use Role::Tiny() in the parent to get apply_roles_to_package().

      Thanks again.


      The way forward always starts with a minimal test.