Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Why Moose roles?

by porton (Initiate)
on Aug 10, 2016 at 12:44 UTC ( [id://1169488]=perlquestion: print w/replies, xml ) Need Help??

porton has asked for the wisdom of the Perl Monks concerning the following question:

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?

Replies are listed 'Best First'.
Re: Why Moose roles?
by trippledubs (Deacon) on Aug 10, 2016 at 14:22 UTC

    Roles are the Perl way to ensure a class adheres to a certain design. In Linux you can OPEN a socket or a file or a device driver or whatever else. They are treated the same because they conform to a certain interface. You might want plug in different sorting algorithms without knowing the details of how they work. A role is just one language's implementation of a concept with the goals of reusing code, minimizing dependency, and designing with flexibility in mind. Sub classes and abstract classes have greater dependency because changing a parent class changes every child class. Often times you'll want to change behavior of a child object, but.. oh wait all that code is in the parent.

    HackaMol - OO Perl for molecular hacking PDF explaining design

    Add Dry-Run testing to classes

    http://blogs.perl.org/users/ovid/2010/01/roles-without-moose.html

Re: Why Moose roles?
by Corion (Patriarch) on Aug 10, 2016 at 12:54 UTC

    I think the idea is that you can combine more than one Role, where combining multiple base classes by inheritance leads to multiple inheritance.

    Never mind that Roles in Perl are usually implemented through (multiple) inheritance, but they seem to placate people who understand OO design better than I do.

Re: Why Moose roles?
by Laurent_R (Canon) on Aug 10, 2016 at 15:52 UTC
    Moose roles have some limitations, such as inability to override a method in a class which "with"es the role.
    It is not a limitation, it is a feature. ;-)

    The whole idea with roles is to avoid the mess occurring quite commonly with multiple inheritance, unseen method resolution conflicts, and so on. Roles make it possible to add behaviors to a class or to objects, not to override existing methods. If you need to override a method, then you have to subclass it.

    Update: fixed a typo.

Re: Why Moose roles?
by Arunbear (Prior) on Aug 11, 2016 at 15:38 UTC
    Role methods can be 'overridden' using an 'around' method modifier.

      Ugh. 'can' ne 'should' is all I can say. :)

      - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1169488]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-03-29 01:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found