APPLYING ROLES

In addition to being applied to a class using the 'with' syntax (see Moose::Manual::Roles) and using the Moose::Util 'apply_all_roles' method, roles may also be applied to an instance of a class using Moose::Util 'apply_all_roles' or the role's metaclass:

MyApp::Test::SomeRole->meta->apply( $instance );

Doing this creates a new, mutable, anonymous subclass, applies the role to that, and reblesses. In a debugger, for example, you will see class names of the form Moose::Meta::Class::__ANON__::SERIAL::6 , which means that doing a 'ref' on your instance may not return what you expect. See Moose::Object for 'DOES'.

CAVEATS

Role support has only a few caveats:

Roles cannot use the extends keyword; it will throw an exception for now. The same is true of the augment and inner keywords (not sure those really make sense for roles). All other Moose keywords will be deferred so that they can be applied to the consuming class.

Role composition does its best to not be order-sensitive when it comes to conflict resolution and requirements detection. However, it is order-sensitive when it comes to method modifiers. All before/around/after modifiers are included whenever a role is composed into a class, and then applied in the order in which the roles are used. This also means that there is no conflict for before/around/after modifiers.

In most cases, this will be a non-issue; however, it is something to keep in mind when using method modifiers in a role. You should never assume any ordering.