Mouse::Role(3pm)

πŸ’œ Mouse::Role β€” the Mouse Role, bestie

Okay so this is Mouse::Role, straight out of the Perl world (perl v5.42.2, ngl), and it's genuinely one of those pages that's like... short but every word eats. Let me walk you through it. 🫢


🌸 VERSION 🌸

This doc describes Mouse version v2.6.2. That's the vibe check on what you're reading, periodt.


✨ SYNOPSIS ✨

Here's the whole glow-up in code, bestie β€” don't touch a character:

package Comparable;
use Mouse::Role; # the package is now a Mouse role

# Declare methods that are required by this role
requires qw(compare);

# Define methods this role provides
sub equals {
    my($self, $other) = @_;
    return $self->compare($other) == 0;
}

# and later
package MyObject;
use Mouse;
with qw(Comparable); # Now MyObject can equals()

sub compare {
    # ...
}

my $foo = MyObject->new();
my $bar = MyObject->new();
$obj->equals($bar); # yes, it is comparable

So like, Comparable is a role, it requires compare, it provides equals. Then MyObject rolls up with use Mouse + with qw(Comparable) and boom β€” it can equals(). That last line even has the little comment # yes, it is comparable and honestly? Iconic. πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

This module declares the caller class to be a Mouse role. Simple as that, no cap.

The whole concept of roles is documented over in Moose::Manual::Roles β€” this page right here is the API documentation, i.e. the "what do the keywords actually do" page. Different job, same universe. πŸ’«


πŸŽ€ EXPORTED FUNCTIONS πŸŽ€

Mouse::Role supports all of the functions that Mouse exports, but it differs slightly in how some items are handled. (Peep the "CAVEATS" section below for the receipts.)

On top of that, Mouse::Role also offers two role-specific keywords, and they are the main characters of this section:

⭐ requires(@method_names) ⭐

Roles can require that certain methods are implemented by any class that "does" the role. So like, if you does the role, you gotta bring those methods. No skipping. πŸ’…

Note that attribute accessors also count as methods for the purposes of satisfying the requirements of a role β€” so don't stress, your accessor can absolutely carry that weight.

⭐ excludes(@role_names) ⭐

This one is exported but not implemented in Mouse. So it's giving "invited to the party but didn't show up." πŸ™ˆ Don't go relying on it.


🌈 IMPORT AND UNIMPORT 🌈

πŸ“ import

Importing Mouse::Role will give you sugar. -traits are also supported. Sweet. 🍬

πŸ“ unimport

Please unimport (no Mouse::Role) so that if someone calls one of the keywords (such as has) it will break loudly instead of breaking subtly. Loud breakage > quiet chaos, fr fr. πŸ“’


πŸ’€ CAVEATS πŸ’€

Role support has only a few caveats. Pay attention, bestie, these matter:

  • Roles cannot use the extends keyword β€” it will throw an exception for now. The same is true of the augment and inner keywords (and honestly, not sure those really make sense for roles anyway 🀷). All other Mouse keywords will be deferred so that they can be applied to the consuming class. Deferred, not deleted β€” important distinction.
  • 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 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. 🚨

Like, genuinely: never assume ordering. That's the whole warning. Don't do it. πŸ’€

πŸ”₯ SEE ALSO πŸ”₯

  • Mouse
  • Moose::Role
  • Moose::Manual::Roles
  • Moose::Spec::Role

All of these are your little study buddies β€” go read them if you want the full lore. πŸ“š


And that's the page, bestie! πŸ’– Big kiss to the Perl crew β€” this doc came from perl v5.42.2, dated 2026-04-21, and it's the Mouse::Role(3pm) manpage. Slay, go write some roles. πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from