Re: Why to use Moose roles ever?
[email protected] (Karen Etheridge) Wed, 10 Aug 2016 14:25:53 -0700
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <CAPJsHfA0-Y=U0+ZgU7jgc0dVz0W_cREXyBV-pr00=082BxfvFw@mail.gmail.com> |
--001a113ddff255d8760539be4bb6
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
> Moose roles have some limitations, such as inability to override a
method in a class which "with"es the role.
This is false. For example:
{
package MyRole;
use Moose::Role;
sub foo {
'I came from the role';
}
}
{
package MyClass;
use Moose;
with 'MyRole';
around foo =3D> sub {
'I came from the class';
};
}
use 5.010;
my $obj =3D MyClass->new;
say $obj->foo;
=E2=80=8B =E2=80=8B
# prints: I came from the class
On Wed, Aug 10, 2016 at 6:00 AM, Victor Porton <[email protected]> wrote:
> Moose roles have some limitations, such as inability to override a
> method in a class which "with"es the role.
>
> But I can use an abstract base class instead of a role.
>
> Are there any real situations where roles are better than base classes?
> With examples, please.
>
--001a113ddff255d8760539be4bb6
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div class=3D"gmail_default" style=3D"font-size:large">>=
; Moose roles have some limitations, such as inability to override a<br>
method in a class which "with"es the role.<br><br></div><div clas=
s=3D"gmail_default" style=3D"font-size:large">This is false. For example:<b=
r><br></div>{<br>=C2=A0=C2=A0=C2=A0 package MyRole;<br>=C2=A0=C2=A0=C2=A0 u=
se Moose::Role;<br>=C2=A0=C2=A0=C2=A0 sub foo {<br>=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0 'I came from the role';<br>=C2=A0=C2=A0=C2=A0 }<=
br>}<br><br>{<br>=C2=A0=C2=A0=C2=A0 package MyClass;<br>=C2=A0=C2=A0=C2=A0 =
use Moose;<br>=C2=A0=C2=A0=C2=A0 with 'MyRole';<br>=C2=A0=C2=A0=C2=
=A0 around foo =3D> sub {<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 'I came =
from the class';<br>=C2=A0=C2=A0=C2=A0 };<br>}<br><br>use 5.010;<br>my =
$obj =3D MyClass->new;<br>say $obj->foo;<div class=3D"gmail_default" =
style=3D"font-size:large;display:inline">=E2=80=8B=C2=A0 =E2=80=8B</div># p=
rints: I came from the class<br><div class=3D"gmail_default" style=3D"font-=
size:large"><br><br></div><div class=3D"gmail_default" style=3D"font-size:l=
arge"><br></div></div><div class=3D"gmail_extra"><br><div class=3D"gmail_qu=
ote">On Wed, Aug 10, 2016 at 6:00 AM, Victor Porton <span dir=3D"ltr"><<=
a href=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a>>=
</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .=
8ex;border-left:1px #ccc solid;padding-left:1ex">Moose roles have some limi=
tations, such as inability to override a<br>
method in a class which "with"es the role.<br>
<br>
But I can use an abstract base class instead of a role.<br>
<br>
Are there any real situations where roles are better than base classes?<br>
With examples, please.<br>
</blockquote></div><br></div>
--001a113ddff255d8760539be4bb6--