Subject: | delegated methods override existing methods |
I would like to delegate a subset of the methods provided by a role to an attribute; the rest of the methods will be provided by the class.
I expect that this should work similarly to using a role or extending a class, namely that if I define a method in my class Moo would recognize that it existed and would not override it.
However, it doesn't look like this is the case.
In the attached code I would like the methods foo, bar, and baz to be handled by class B. B::baz correctly overrides My::Role2::baz, but My::Role::foo is called via the "ind" attribute instead of B::foo.
I need to explicitly override the delegation of B::bar after creating the "ind" attribute in order for B::bar to be called rather than My::Role::bar.
I expect that this should work similarly to using a role or extending a class, namely that if I define a method in my class Moo would recognize that it existed and would not override it.
However, it doesn't look like this is the case.
In the attached code I would like the methods foo, bar, and baz to be handled by class B. B::baz correctly overrides My::Role2::baz, but My::Role::foo is called via the "ind" attribute instead of B::foo.
I need to explicitly override the delegation of B::bar after creating the "ind" attribute in order for B::bar to be called rather than My::Role::bar.
Subject: | handles.pl |
{
package My::Role;
use Moo::Role;
sub foo { print __PACKAGE__, "::foo\n" };
sub bar { print __PACKAGE__, "::bar\n" };
}
{ package My::Role2;
use Moo::Role;
sub baz { print __PACKAGE__, "::baz\n" };
}
{
package A;
use Moo;
with 'My::Role';
}
{ package B;
use Moo;
sub foo { print __PACKAGE__, "::foo\n" };
sub baz { print __PACKAGE__, "::baz\n" };
with 'My::Role2';
has ind => ( is => 'ro',
handles => 'My::Role',
default => sub { A->new }
);
no warnings 'redefine', 'once';
*bar = sub { print __PACKAGE__, "::bar\n" };
}
B->new->foo;
B->new->bar;
B->new->baz;