1Mouse::Role(3) User Contributed Perl Documentation Mouse::Role(3)
2
3
4
6 Mouse::Role - The Mouse Role
7
9 This document describes Mouse version v2.5.10
10
12 package Comparable;
13 use Mouse::Role; # the package is now a Mouse role
14
15 # Declare methods that are required by this role
16 requires qw(compare);
17
18 # Define methods this role provides
19 sub equals {
20 my($self, $other) = @_;
21 return $self->compare($other) == 0;
22 }
23
24 # and later
25 package MyObject;
26 use Mouse;
27 with qw(Comparable); # Now MyObject can equals()
28
29 sub compare {
30 # ...
31 }
32
33 my $foo = MyObject->new();
34 my $bar = MyObject->new();
35 $obj->equals($bar); # yes, it is comparable
36
38 This module declares the caller class to be a Mouse role.
39
40 The concept of roles is documented in Moose::Manual::Roles. This
41 document serves as API documentation.
42
44 Mouse::Role supports all of the functions that Mouse exports, but
45 differs slightly in how some items are handled (see "CAVEATS" below for
46 details).
47
48 Mouse::Role also offers two role-specific keywords:
49
50 "requires(@method_names)"
51 Roles can require that certain methods are implemented by any class
52 which "does" the role.
53
54 Note that attribute accessors also count as methods for the purposes of
55 satisfying the requirements of a role.
56
57 "excludes(@role_names)"
58 This is exported but not implemented in Mouse.
59
61 import
62 Importing Mouse::Role will give you sugar. "-traits" are also
63 supported.
64
65 unimport
66 Please unimport ("no Mouse::Role") so that if someone calls one of the
67 keywords (such as "has") it will break loudly instead breaking subtly.
68
70 Role support has only a few caveats:
71
72 · Roles cannot use the "extends" keyword; it will throw an exception
73 for now. The same is true of the "augment" and "inner" keywords
74 (not sure those really make sense for roles). All other Mouse
75 keywords will be deferred so that they can be applied to the
76 consuming class.
77
78 · Role composition does its best to not be order-sensitive when it
79 comes to conflict resolution and requirements detection. However,
80 it is order-sensitive when it comes to method modifiers. All
81 before/around/after modifiers are included whenever a role is
82 composed into a class, and then applied in the order in which the
83 roles are used. This also means that there is no conflict for
84 before/around/after modifiers.
85
86 In most cases, this will be a non-issue; however, it is something
87 to keep in mind when using method modifiers in a role. You should
88 never assume any ordering.
89
91 Mouse
92
93 Moose::Role
94
95 Moose::Manual::Roles
96
97 Moose::Spec::Role
98
99
100
101perl v5.32.0 2020-07-28 Mouse::Role(3)