Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Way to use Moo::Role to extend non-Moo class?

by Arunbear (Parson)
on Nov 13, 2013 at 10:49 UTC ( #1062370=note: print w/ replies, xml ) Need Help??


in reply to Way to use Moo::Role to extend non-Moo class?

What is the purpose of your bar package? If you wanted to reuse methods from package foo without refactoring foo itself, you could use method forwarding:

package bar; use Moo::Role; has 'foo' => ( is => 'rwp', handles => [qw/foo/], # put other methods to be delegated here );
Then any class that consumes this role will be able to call the methods of foo that you set up delegation for.

If you were wanting to split up the methods into different roles, you could e.g. move the foo() method to its own role:

package Foo::Role; sub foo { print "foo\n"; }
Then if bar wanted to 'extend' this with other methods:
package bar; use Moo::Role; with 'Foo::Role'; # ... other methods


Comment on Re: Way to use Moo::Role to extend non-Moo class?
Select or Download Code
Re^2: Way to use Moo::Role to extend non-Moo class?
by einhverfr (Pilgrim) on Nov 13, 2013 at 11:07 UTC
    In my case it won't be that simple, but it's very helpful in terms of thinking through the problem. Thanks.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-10-25 15:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (145 votes), past polls