Moose::Meta::Role(3) User Contributed Perl Documentation Moose::Meta::Role(3)

# [DESCRIPTION](#DESCRIPTION)

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.

# [INHERITANCE](#INHERITANCE)

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

# [METHODS](#METHODS)

## [Construction](#Construction)

*Moose::Meta::Role-&gt;initialize($role\_name)*

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

*Moose::Meta::Role-&gt;combine( \[ $role =&gt; { ... } ], \[ $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.

*$metarole**-&gt;composition\_class\_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-&gt;create($name, %options)*

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

*Moose::Meta::Role-&gt;create\_anon\_role*

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

*$metarole**-&gt;is\_anon\_role*

Returns true if the role is an anonymous role.

*$metarole**-&gt;consumers*

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

## [Role application](#Role_application)

*$metarole**-&gt;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](#Roles_and_other_roles)

*$metarole**-&gt;get\_roles*

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

*$metarole**-&gt;calculate\_all\_roles*

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

*$metarole**-&gt;does\_role($role)*

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

*$metarole**-&gt;add\_role($role)*

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

*$metarole**-&gt;get\_excluded\_roles\_list*

Returns a list of role names which this role excludes.

*$metarole**-&gt;excludes\_role($role\_name)*

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

*$metarole**-&gt;add\_excluded\_roles(@role\_names)*

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

## [Methods](#Methods)

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

*$metarole**-&gt;method\_metaclass*

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

*$metarole**-&gt;get\_method($name)*

*$metarole**-&gt;has\_method($name)*

*$metarole**-&gt;add\_method( $name, $body )*

*$metarole**-&gt;get\_method\_list*

*$metarole**-&gt;find\_method\_by\_name($name)*

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

## [Attributes](#Attributes)

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**-&gt;get\_attribute($attribute\_name)*

*$metarole**-&gt;has\_attribute($attribute\_name)*

*$metarole**-&gt;get\_attribute\_list*

*$metarole**-&gt;add\_attribute($name, %options)*

*$metarole**-&gt;remove\_attribute($attribute\_name)*

## [Overload introspection and creation](#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**-&gt;is\_overloaded*

*$metarole**-&gt;get\_overloaded\_operator($op)*

*$metarole**-&gt;has\_overloaded\_operator($op)*

*$metarole**-&gt;get\_overload\_list*

*$metarole**-&gt;get\_all\_overloaded\_operators*

*$metarole**-&gt;add\_overloaded\_operator($op, $impl)*

*$metarole**-&gt;remove\_overloaded\_operator($op)*

## [Required methods](#Required_methods)

*$metarole**-&gt;get\_required\_method\_list*

Returns the list of methods required by the role.

*$metarole**-&gt;requires\_method($name)*

Returns true if the role requires the named method.

*$metarole**-&gt;add\_required\_methods(@names)*

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

*$metarole**-&gt;remove\_required\_methods(@names)*

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

*$metarole**-&gt;add\_conflicting\_method(%params)*

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

## [Method modifiers](#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**-&gt;add\_after\_method\_modifier($method\_name, $method)*

*$metarole**-&gt;add\_around\_method\_modifier($method\_name, $method)*

*$metarole**-&gt;add\_before\_method\_modifier($method\_name, $method)*

*$metarole**-&gt;add\_override\_method\_modifier($method\_name, $method)*

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

*$metarole**-&gt;has\_after\_method\_modifiers*

*$metarole**-&gt;has\_around\_method\_modifiers*

*$metarole**-&gt;has\_before\_method\_modifiers*

*$metarole**-&gt;has\_override\_method\_modifier*

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

*$metarole**-&gt;get\_after\_method\_modifiers($method\_name)*

*$metarole**-&gt;get\_around\_method\_modifiers($method\_name)*

*$metarole**-&gt;get\_before\_method\_modifiers($method\_name)*

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

*$metarole**-&gt;get\_override\_method\_modifier($method\_name)*

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

## [Introspection](#Introspection)

*Moose::Meta::Role-&gt;meta*

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

# [BUGS](#BUGS)

See "BUGS" in Moose for details on reporting bugs.

2026-10-01 perl v5.44.0

---

Document: Moose::Meta::Role(3pm) from perl-moose 2.4001-r0 (Chainguard OS)
Source: https://github.com/moose/Moose
