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

Re: Why to use Moose roles ever?

Thread Previous | Thread Next
From:
Karen Etheridge
Date:
August 10, 2016 21:26
Subject:
Re: Why to use Moose roles ever?
Message ID:
CAPJsHfA0-Y=U0+ZgU7jgc0dVz0W_cREXyBV-pr00=082BxfvFw@mail.gmail.com
> 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 => sub {
      'I came from the class';
    };
}

use 5.010;
my $obj = MyClass->new;
say $obj->foo;
​  ​
# prints: I came from the class




On Wed, Aug 10, 2016 at 6:00 AM, Victor Porton <porton@narod.ru> 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.
>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About