in reply to Re^2: Is there a better way to do this?
in thread Is there a better way to do this?
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;
Output: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;
$ 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() :
Output: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;
$ perl -MMyClass -E '$o = MyClass->new; say $o->foo, $o->bar;' foobar
Thanks again for the pointer.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Is there a better way to do this?
by choroba (Cardinal) on Mar 19, 2016 at 17:40 UTC | |
by 1nickt (Canon) on Mar 19, 2016 at 18:31 UTC | |
by choroba (Cardinal) on Mar 19, 2016 at 18:45 UTC | |
by 1nickt (Canon) on Mar 19, 2016 at 19:49 UTC | |
by Your Mother (Archbishop) on Apr 09, 2019 at 02:43 UTC |