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

Moo::Role is based on Role::Tiny. It means you can use
'Moo::Role'->apply_roles_to_package(...);

There's no need to use Role::Tiny from Moo.

($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,

Replies are listed 'Best First'.
Re^5: Is there a better way to do this?
by 1nickt (Canon) on Mar 19, 2016 at 18:31 UTC

    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.
      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.
Re^5: Is there a better way to do this?
by Your Mother (Archbishop) on Apr 09, 2019 at 02:43 UTC

    Thanks, guys. This bit me tonight and the answer was exactly what you suggested.