Section: User Contributed Perl Documentation (3)
Updated: 2018-05-16
Page Index


Moose::Meta::Role - The Moose Role metaclass  


version 2.2011  


This class is a subclass of Class::MOP::Module that provides additional Moose-specific functionality.

Its API looks a lot like Moose::Meta::Class, but internally it implements many things differently. This may change in the future.  


"Moose::Meta::Role" is a subclass of Class::MOP::Module.  





This method creates a new role object with the provided name.

Moose::Meta::Role->combine( [ $role => { ... } ], [ $role ], ... )

This method accepts a list of array references. Each array reference should contain a role name or Moose::Meta::Role object as its first element. The second element is an optional hash reference. The hash reference can contain "-excludes" and "-alias" keys to control how methods are composed from the role.

The return value is a new Moose::Meta::Role::Composite that represents the combined roles.


When combining multiple roles using "combine", this method is used to obtain a list of role names to be applied to the Moose::Meta::Role::Composite instance returned by "combine". The default implementation returns an empty list. Extensions that need to hook into role combination may wrap this method to return additional role names.

Moose::Meta::Role->create($name, %options)

This method is identical to the Moose::Meta::Class "create" method.


This method is identical to the Moose::Meta::Class "create_anon_class" method.


Returns true if the role is an anonymous role.


Returns a list of names of classes and roles which consume this role.  

Role application

$metarole->apply( $thing, @options )

This method applies a role to the given $thing. That can be another Moose::Meta::Role, object, a Moose::Meta::Class object, or a (non-meta) object instance.

The options are passed directly to the constructor for the appropriate Moose::Meta::Role::Application subclass.

Note that this will apply the role even if the $thing in question already "does" this role. ``does_role'' in Moose::Util is a convenient wrapper for finding out if role application is necessary.  

Roles and other roles


This returns an array reference of roles which this role does. This list may include duplicates.


This returns a unique list of all roles that this role does, and all the roles that its roles do.


Given a role name or Moose::Meta::Role object, returns true if this role does the given role.


Given a Moose::Meta::Role object, this adds the role to the list of roles that the role does.


Returns a list of role names which this role excludes.


Given a role name, returns true if this role excludes the named role.


Given one or more role names, adds those roles to the list of excluded roles.  


The methods for dealing with a role's methods are all identical in API and behavior to the same methods in Class::MOP::Class.


Returns the method metaclass name for the role. This defaults to Moose::Meta::Role::Method.



$metarole->add_method( $name, $body )



These methods are all identical to the methods of the same name in Class::MOP::Package  


As with methods, the methods for dealing with a role's attribute are all identical in API and behavior to the same methods in Class::MOP::Class.

However, attributes stored in this class are not stored as objects. Rather, the attribute definition is stored as a hash reference. When a role is composed into a class, this hash reference is passed directly to the metaclass's "add_attribute" method.

This is quite likely to change in the future.




$metarole->add_attribute($name, %options)


Overload introspection and creation

The methods for dealing with a role's overloads are all identical in API and behavior to the same methods in Class::MOP::Class.






$metarole->add_overloaded_operator($op, $impl)


Required methods


Returns the list of methods required by the role.


Returns true if the role requires the named method.


Adds the named methods to the role's list of required methods.


Removes the named methods from the role's list of required methods.


Instantiate the parameters as a Moose::Meta::Role::Method::Conflicting object, then add it to the required method list.  

Method modifiers

These methods act like their counterparts in Class::MOP::Class and Moose::Meta::Class.

However, method modifiers are simply stored internally, and are not applied until the role itself is applied to a class or object.

$metarole->add_after_method_modifier($method_name, $method)

$metarole->add_around_method_modifier($method_name, $method)

$metarole->add_before_method_modifier($method_name, $method)

$metarole->add_override_method_modifier($method_name, $method)

These methods all add an appropriate modifier to the internal list of modifiers.





Return true if the role has any modifiers of the given type.




Given a method name, returns a list of the appropriate modifiers for that method.


Given a method name, returns the override method modifier for that method, if it has one.  



This will return a Class::MOP::Class instance for this class.  


See ``BUGS'' in Moose for details on reporting bugs.  


Stevan Little <>
Dave Rolsky <>
Jesse Luehrs <>
Shawn M Moore <>
יובל קוג'מן (Yuval Kogman) <>
Karen Etheridge <>
Florian Ragwitz <>
Hans Dieter Pearcey <>
Chris Prather <>
Matt S Trout <>


This software is copyright (c) 2006 by Infinity Interactive, Inc.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.



Role application
Roles and other roles
Overload introspection and creation
Required methods
Method modifiers
LinuxReviews : manual page archive : man3pm