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

Instead of evaling a with, you can also apply_roles_to_package or apply_roles_to_object .
($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^3: Is there a better way to do this?
by 1nickt (Canon) on Mar 19, 2016 at 16:35 UTC

    Thanks choroba, I didn't know about those Role::Tiny methods.

    I am not sure about whether I prefer them to eval "with '$module'; 1;", though.

    I had to load Role::Tiny explicitly not importing anything, since its before(), after(), around() and with() methods clash with Moo's :

    Subroutine MyClass::before redefined at [...]/5.22.0/Role/Tiny.pm line + 58. Subroutine MyClass::after redefined at [...]/5.22.0/Role/Tiny.pm line +58. Subroutine MyClass::around redefined at [...]/5.22.0/Role/Tiny.pm line + 58. Subroutine MyClass::with redefined at [...]/5.22.0/Role/Tiny.pm line 6 +7.

    And even when I do that I can't use apply_roles_to_object() inside sub BUILD, because the parent's methods haven't been loaded and thus the child's will be.

    package MyClass::Child; use Moo::Role; sub foo { die 'horribly'; } sub bar { return 'bar'; } 1;
    package MyClass; use Moo; use Role::Tiny(); sub BUILD { my $self = shift; Role::Tiny->apply_roles_to_object( $self, 'MyClass::Child' ); return $self; }; sub foo { return 'foo'; } 1;
    Output:
    $ perl -MMyClass -E '$o = MyClass->new; say $o->foo, $o->bar;' horribly at MyClass/Child.pm line 5.

    So the only way I could make it work as part of the Moo constructor was with apply_roles_to_package() :

    package MyClass; use Moo; use Role::Tiny(); sub BUILD { my $self = shift; Role::Tiny->apply_roles_to_package( __PACKAGE__, 'MyClass::Child' +); return $self; }; sub foo { return 'foo'; } 1;
    Output:
    $ perl -MMyClass -E '$o = MyClass->new; say $o->foo, $o->bar;' foobar

    Thanks again for the pointer.


    The way forward always starts with a minimal test.
      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,

        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.

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