Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re^5: Delegating to a role in Moose

by Arunbear (Prior)
on Jan 13, 2018 at 18:14 UTC ( #1207200=note: print w/replies, xml ) Need Help??

in reply to Re^4: Delegating to a role in Moose
in thread Delegating to a role in Moose

Well this is what the docs say:
With the role option, you specify the name of a role or a role type whose "interface" then becomes the list of methods to handle. The "interface" can be defined as; the methods of the role and any required methods of the role. It should be noted that this does not include any method modifiers or generated attribute methods (which is consistent with role composition).
So in your example
has 'uri' => ( is => 'ro', isa => 'URI', handles => 'MyURIRole', );
means that the methods of MyURIRole i.e. 'host', 'path' and 'print_hi' will be delegated to the 'uri' attribute, which in this case is an object of class "URI::http" and that class doesn't provide a "print_hi" method.

To reiterate, MyURIRole is only being used by 'handles' in an informational way (i.e. to determine the names of delegated methods), it does not use the role to provide the method implementations.

Replies are listed 'Best First'.
Re^6: Delegating to a role in Moose
by nysus (Vicar) on Jan 13, 2018 at 21:57 UTC

    Alright, thanks. I guess my only question left, then, is why would you use something like handles => 'MyURIRole' over handles => qr/.*/? The latter seems a lot more useful and it does allow me to extend the URI class with my own methods very easily and which seems enormously useful. However, I found it very odd that after doing a lot of searching on this, there is only one tutorial I could find that even mentions the handles => qr/.*/ method for delegating. The omission from the documentation of this method for delegating makes me wonder if it's advisable to do (and hence why I looked into using a more "official" way by using a role name instead).

    Overall, it just seems the documentation on delegation is lacking a bit, especially for someone like me who doesn't have a lot of experience with OO patterns and is more or less just following recipes without understanding the chemistry behind the cooking. Maybe I should reach out to the module authors? What do you think?

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

      Re: handles => 'MyURIRole' vs handles => qr/.*/, the former is more work to set up, but I think it should be preferred because it's more self documenting - you've documented in code exactly what methods you're delegating.

      I agree that the documentation on delegation is lacking, but that's a symptom of a larger problem. E.g. the official docs on Perl OOP, perlobj and perlootut don't mention delegation at all.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1207200]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2020-09-30 21:59 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (170 votes). Check out past polls.