1Moose::Role(3) User Contributed Perl Documentation Moose::Role(3)
2
3
4
6 Moose::Role - The Moose Role
7
9 version 2.2203
10
12 package Eq;
13 use Moose::Role; # automatically turns on strict and warnings
14
15 requires 'equal';
16
17 sub no_equal {
18 my ($self, $other) = @_;
19 !$self->equal($other);
20 }
21
22 # ... then in your classes
23
24 package Currency;
25 use Moose; # automatically turns on strict and warnings
26
27 with 'Eq';
28
29 sub equal {
30 my ($self, $other) = @_;
31 $self->as_float == $other->as_float;
32 }
33
34 # ... and also
35
36 package Comparator;
37 use Moose;
38
39 has compare_to => (
40 is => 'ro',
41 does => 'Eq',
42 handles => 'Eq',
43 );
44
45 # ... which allows
46
47 my $currency1 = Currency->new(...);
48 my $currency2 = Currency->new(...);
49 Comparator->new(compare_to => $currency1)->equal($currency2);
50
52 The concept of roles is documented in Moose::Manual::Roles. This
53 document serves as API documentation.
54
56 Moose::Role currently supports all of the functions that Moose exports,
57 but differs slightly in how some items are handled (see "CAVEATS" below
58 for details).
59
60 Moose::Role also offers two role-specific keyword exports:
61
62 requires (@method_names)
63 Roles can require that certain methods are implemented by any class
64 which "does" the role.
65
66 Note that attribute accessors also count as methods for the purposes of
67 satisfying the requirements of a role.
68
69 excludes (@role_names)
70 Roles can "exclude" other roles, in effect saying "I can never be
71 combined with these @role_names". This is a feature which should not be
72 used lightly.
73
74 no Moose::Role
75 Moose::Role offers a way to remove the keywords it exports, through the
76 "unimport" method. You simply have to say "no Moose::Role" at the
77 bottom of your code for this to work.
78
80 When you use Moose::Role, you can specify traits which will be applied
81 to your role metaclass:
82
83 use Moose::Role -traits => 'My::Trait';
84
85 This is very similar to the attribute traits feature. When you do this,
86 your class's "meta" object will have the specified traits applied to
87 it. See "Metaclass and Trait Name Resolution" in Moose for more
88 details.
89
90 All role metaclasses (note, not the role itself) extend
91 Moose::Meta::Role. You can test if a package is a role or not using
92 "is_role" in Moose::Util.
93
95 In addition to being applied to a class using the 'with' syntax (see
96 Moose::Manual::Roles) and using the Moose::Util 'apply_all_roles'
97 method, roles may also be applied to an instance of a class using
98 Moose::Util 'apply_all_roles' or the role's metaclass:
99
100 MyApp::Test::SomeRole->meta->apply( $instance );
101
102 Doing this creates a new, mutable, anonymous subclass, applies the role
103 to that, and reblesses. In a debugger, for example, you will see class
104 names of the form " Moose::Meta::Class::__ANON__::SERIAL::6 ", which
105 means that doing a 'ref' on your instance may not return what you
106 expect. See Moose::Object for 'DOES'.
107
108 Additional params may be added to the new instance by providing
109 'rebless_params'. See Moose::Meta::Role::Application::ToInstance.
110
112 Role support has only a few caveats:
113
114 • Roles cannot use the "extends" keyword; it will throw an exception
115 for now. The same is true of the "augment" and "inner" keywords
116 (not sure those really make sense for roles). All other Moose
117 keywords will be deferred so that they can be applied to the
118 consuming class.
119
120 • Role composition does its best to not be order-sensitive when it
121 comes to conflict resolution and requirements detection. However,
122 it is order-sensitive when it comes to method modifiers. All
123 before/around/after modifiers are included whenever a role is
124 composed into a class, and then applied in the order in which the
125 roles are used. This also means that there is no conflict for
126 before/around/after modifiers.
127
128 In most cases, this will be a non-issue; however, it is something
129 to keep in mind when using method modifiers in a role. You should
130 never assume any ordering.
131
133 See "BUGS" in Moose for details on reporting bugs.
134
136 • Stevan Little <stevan@cpan.org>
137
138 • Dave Rolsky <autarch@urth.org>
139
140 • Jesse Luehrs <doy@cpan.org>
141
142 • Shawn M Moore <sartak@cpan.org>
143
144 • יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
145
146 • Karen Etheridge <ether@cpan.org>
147
148 • Florian Ragwitz <rafl@debian.org>
149
150 • Hans Dieter Pearcey <hdp@cpan.org>
151
152 • Chris Prather <chris@prather.org>
153
154 • Matt S Trout <mstrout@cpan.org>
155
157 This software is copyright (c) 2006 by Infinity Interactive, Inc.
158
159 This is free software; you can redistribute it and/or modify it under
160 the same terms as the Perl 5 programming language system itself.
161
162
163
164perl v5.36.0 2023-02-06 Moose::Role(3)