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

Howdy, Perl Monks!

I'm trying to implement roles in our extensive codebase using Role::Tiny, but it's not behaving as expected. CPAN clearly states: "If a method is already defined on a class, that method will not be composed in from the role.". However in my current code it is clear that the method in the role is being used.

My code works as follows (package naming simplified for clarity):

  1. I have a parent package Generic, a child package File, and a role Draw.
  2. Both File and Draw have a method called 'render' which is identical apart from one line that adds text to the image (in the child) or omits it (in the role).
  3. After instantiation, the File object calls a method init from its parent which uses Role::Tiny::apply_roles_to_object, which duly changes the class from File to File__WITH__Role::Draw.
  4. However when the render method is called, the image has no text. The method from Draw is being used, even though there is a render method in File.

I can only assume that something arcane is happening with the combination of standard inheritance and roles, but I can't put my finger on it...

Replies are listed 'Best First'.
Re: Role::Tiny not behaving as expected
by tangent (Parson) on Feb 29, 2016 at 20:47 UTC
    I think the problem is with your use of apply_roles_to_object. I did a little test and if you change the method to apply_roles_to_package it works they way you expect:
    print "Using apply_roles_to_object\n"; my $test_1 = File->new; print "test_1->render\n"; $test_1->render; Role::Tiny->apply_roles_to_object($test_1, 'Draw'); print "test_1->render after apply_roles_to_object\n"; $test_1->render; print "\n\n"; print "Using apply_roles_to_package\n"; my $test_2 = File->new; print "test_2->render\n"; $test_2->render; Role::Tiny->apply_roles_to_package('File', 'Draw'); print "test_2->render after apply_roles_to_package\n"; $test_2->render;
    Output:
    Using apply_roles_to_object test_1->render This is package 'File' - render test_1->render after apply_roles_to_object This is package 'Draw' - render Using apply_roles_to_package test_2->render This is package 'File' - render test_2->render after apply_roles_to_package This is package 'File' - render
      Thanks - I will try this out!
Re: Role::Tiny not behaving as expected
by Haarg (Priest) on Mar 01, 2016 at 08:19 UTC

    "If a method is already defined on a class" is meant to mean that methods that exist directly in the class will not be overwritten. However, methods in superclasses will be overridden by roles. The way apply_roles_to_object works is to create a new subclass, apply the role to it, and re-bless the object into the subclass. Since the role is being applied to the subclass, it will override the methods defined directly in the initial class.

    So the behavior is all basically consistent, and it also matches what my personal expectations are. I can see how people would expect something different though. The documentation around this definitely needs improvements.

    To work around this issue, my usual recommendation would be to avoid apply_roles_to_object. Instead, I'd try to generate all of the classes you need up front, and have a method to pick the correct class to instantiate.

      Ah, thanks for the explanation - I agree that the documentation isn't crystal clear. I'll have a rethink of how we mix roles and subclesses.
Re: Role::Tiny not behaving as expected
by choroba (Cardinal) on Mar 03, 2016 at 19:21 UTC
    I've been bitten by the same problem recently, and documented my adventure here: Role Composition versus Inheritance. I also submitted a pull request to improve the documentation, which has already been accepted.
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,