tags:

views:

81

answers:

2

I'd like to set up a convenience Moose role made up of other smaller roles. For example, if I have WithAddress and WithPhone I would like a single WithContacts that provides both WithAddress and WithPhone and anything contact methods I add in the future.

How can I do this with Moose?

+7  A: 
package WithContacts;

use Moose::Role;
with qw(WithAddress WithPhone);

# anything else your role should do

no Moose::Role;
1;
cjm
*Ker-DUH!* Does the `no Moose::Role` go after all the methods are defined?
Schwern
@Schwern, normally you put `no Moose` or `no Moose::Role` immediately before the end of the file. I think you can put it anywhere after you're done with `with` and `has` and `requires`, etc., but end-of-file is simplest.
cjm
It's not really needed at all; it just unimports the various sugar like 'has', 'with' etc. `use namespace::autoclean;` is arguably better (a best-practice on this isn't really figured out yet though).
Ether
If you're afraid of magic, and you're not using anything that imports functions *besides* Moose[::Role], then letting Moose do the unimport seems perfectly sensible. namespace::autoclean shines more when there's a big mess to clean up :)
hobbs
A: 

@Schwern - thanks for asking - I thought I tried this and had a problem and it's been nagging me. Now I have the courage to try again!

Sean Blanton