1Role::Tiny(3) User Contributed Perl Documentation Role::Tiny(3)
2
3
4
6 Role::Tiny - Roles: a nouvelle cuisine portion size slice of Moose
7
9 package Some::Role;
10
11 use Role::Tiny;
12
13 sub foo { ... }
14
15 sub bar { ... }
16
17 around baz => sub { ... };
18
19 1;
20
21 elsewhere
22
23 package Some::Class;
24
25 use Role::Tiny::With;
26
27 # bar gets imported, but not foo
28 with 'Some::Role';
29
30 sub foo { ... }
31
32 # baz is wrapped in the around modifier by Class::Method::Modifiers
33 sub baz { ... }
34
35 1;
36
37 If you wanted attributes as well, look at Moo::Role.
38
40 "Role::Tiny" is a minimalist role composition tool.
41
43 Role composition can be thought of as much more clever and meaningful
44 multiple inheritance. The basics of this implementation of roles is:
45
46 • If a method is already defined on a class, that method will not be
47 composed in from the role. A method inherited by a class gets
48 overridden by the role's method of the same name, though.
49
50 • If a method that the role "requires" to be implemented is not
51 implemented, role application will fail loudly.
52
53 Unlike Class::C3, where the last class inherited from "wins," role
54 composition is the other way around, where the class wins. If multiple
55 roles are applied in a single call (single with statement), then if any
56 of their provided methods clash, an exception is raised unless the
57 class provides a method since this conflict indicates a potential
58 problem.
59
60 ROLE METHODS
61 All subs created after importing Role::Tiny will be considered methods
62 to be composed. For example:
63
64 package MyRole;
65 use List::Util qw(min);
66 sub mysub { }
67 use Role::Tiny;
68 use List::Util qw(max);
69 sub mymethod { }
70
71 In this role, "max" and "mymethod" will be included when composing
72 MyRole, and "min" and "mysub" will not. For additional control,
73 namespace::clean can be used to exclude undesired subs from roles.
74
76 requires
77 requires qw(foo bar);
78
79 Declares a list of methods that must be defined to compose role.
80
81 with
82 with 'Some::Role1';
83
84 with 'Some::Role1', 'Some::Role2';
85
86 Composes another role into the current role (or class via
87 Role::Tiny::With).
88
89 If you have conflicts and want to resolve them in favour of Some::Role1
90 you can instead write:
91
92 with 'Some::Role1';
93 with 'Some::Role2';
94
95 If you have conflicts and want to resolve different conflicts in favour
96 of different roles, please refactor your codebase.
97
98 before
99 before foo => sub { ... };
100
101 See "before method(s) => sub { ... };" in Class::Method::Modifiers for
102 full documentation.
103
104 Note that since you are not required to use method modifiers,
105 Class::Method::Modifiers is lazily loaded and we do not declare it as a
106 dependency. If your Role::Tiny role uses modifiers you must depend on
107 both Class::Method::Modifiers and Role::Tiny.
108
109 around
110 around foo => sub { ... };
111
112 See "around method(s) => sub { ... };" in Class::Method::Modifiers for
113 full documentation.
114
115 Note that since you are not required to use method modifiers,
116 Class::Method::Modifiers is lazily loaded and we do not declare it as a
117 dependency. If your Role::Tiny role uses modifiers you must depend on
118 both Class::Method::Modifiers and Role::Tiny.
119
120 after
121 after foo => sub { ... };
122
123 See "after method(s) => sub { ... };" in Class::Method::Modifiers for
124 full documentation.
125
126 Note that since you are not required to use method modifiers,
127 Class::Method::Modifiers is lazily loaded and we do not declare it as a
128 dependency. If your Role::Tiny role uses modifiers you must depend on
129 both Class::Method::Modifiers and Role::Tiny.
130
131 Strict and Warnings
132 In addition to importing subroutines, using "Role::Tiny" applies strict
133 and warnings to the caller.
134
136 does_role
137 if (Role::Tiny::does_role($foo, 'Some::Role')) {
138 ...
139 }
140
141 Returns true if class has been composed with role.
142
143 This subroutine is also installed as ->does on any class a Role::Tiny
144 is composed into unless that class already has an ->does method, so
145
146 if ($foo->does('Some::Role')) {
147 ...
148 }
149
150 will work for classes but to test a role, one must use ::does_role
151 directly.
152
153 Additionally, Role::Tiny will override the standard Perl "DOES" method
154 for your class. However, if "any" class in your class' inheritance
155 hierarchy provides "DOES", then Role::Tiny will not override it.
156
158 make_role
159 Role::Tiny->make_role('Some::Role');
160
161 Makes a package into a role, but does not export any subs into it.
162
163 apply_roles_to_package
164 Role::Tiny->apply_roles_to_package(
165 'Some::Package', 'Some::Role', 'Some::Other::Role'
166 );
167
168 Composes role with package. See also Role::Tiny::With.
169
170 apply_roles_to_object
171 Role::Tiny->apply_roles_to_object($foo, qw(Some::Role1 Some::Role2));
172
173 Composes roles in order into object directly. Object is reblessed into
174 the resulting class. Note that the object's methods get overridden by
175 the role's ones with the same names.
176
177 create_class_with_roles
178 Role::Tiny->create_class_with_roles('Some::Base', qw(Some::Role1 Some::Role2));
179
180 Creates a new class based on base, with the roles composed into it in
181 order. New class is returned.
182
183 is_role
184 Role::Tiny->is_role('Some::Role1')
185
186 Returns true if the given package is a role.
187
189 • On perl 5.8.8 and earlier, applying a role to an object won't apply
190 any overloads from the role to other copies of the object.
191
192 • On perl 5.16 and earlier, applying a role to a class won't apply
193 any overloads from the role to any existing instances of the class.
194
196 Role::Tiny is the attribute-less subset of Moo::Role; Moo::Role is a
197 meta-protocol-less subset of the king of role systems, Moose::Role.
198
199 Ovid's Role::Basic provides roles with a similar scope, but without
200 method modifiers, and having some extra usage restrictions.
201
203 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
204
206 dg - David Leadbeater (cpan:DGL) <dgl@dgl.cx>
207
208 frew - Arthur Axel "fREW" Schmidt (cpan:FREW) <frioux@gmail.com>
209
210 hobbs - Andrew Rodland (cpan:ARODLAND) <arodland@cpan.org>
211
212 jnap - John Napiorkowski (cpan:JJNAPIORK) <jjn1056@yahoo.com>
213
214 ribasushi - Peter Rabbitson (cpan:RIBASUSHI) <ribasushi@cpan.org>
215
216 chip - Chip Salzenberg (cpan:CHIPS) <chip@pobox.com>
217
218 ajgb - Alex J. G. Burzyński (cpan:AJGB) <ajgb@cpan.org>
219
220 doy - Jesse Luehrs (cpan:DOY) <doy at tozt dot net>
221
222 perigrin - Chris Prather (cpan:PERIGRIN) <chris@prather.org>
223
224 Mithaldu - Christian Walde (cpan:MITHALDU)
225 <walde.christian@googlemail.com>
226
227 ilmari - Dagfinn Ilmari Mannsåker (cpan:ILMARI) <ilmari@ilmari.org>
228
229 tobyink - Toby Inkster (cpan:TOBYINK) <tobyink@cpan.org>
230
231 haarg - Graham Knop (cpan:HAARG) <haarg@haarg.org>
232
234 Copyright (c) 2010-2012 the Role::Tiny "AUTHOR" and "CONTRIBUTORS" as
235 listed above.
236
238 This library is free software and may be distributed under the same
239 terms as perl itself.
240
241
242
243perl v5.34.0 2021-07-22 Role::Tiny(3)