develooper Front page | perl.moose | Postings from August 2016

Re: Why to use Moose roles ever?

Thread Previous | Thread Next
Niall Young
August 11, 2016 03:43
Re: Why to use Moose roles ever?
Message ID:
On Wed, 10 Aug 2016, Victor Porton 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.

I was going to suggest augment'ing a method in a Role, which I'd been planning to explore as a way of declaring interfaces and standard parameter/type checks in Classes, while consuming Roles for the implementation, but alas:

 	"Roles cannot support 'augment' at ..."

> This is false. For example:

Not sure how your example applies here Karen, there is no foo() declared in MyClass only a foo method modifier, and MyRole's foo() doesn't appear to be executed from your output?

>     package MyClass;
>     use Moose;
>     with 'MyRole';
>     around foo => sub {
>       'I came from the class';
>     };

> say $obj->foo;​ 
> ​# prints: I came from the class

Niall Young
Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About