Commit | Line | Data |
ab3370e7 |
1 | package Role::Tiny; |
2 | |
119014a7 |
3 | sub _getglob { \*{$_[0]} } |
5e03b55c |
4 | sub _getstash { \%{"$_[0]::"} } |
119014a7 |
5 | |
b1eebd55 |
6 | use strict; |
7 | use warnings FATAL => 'all'; |
ab3370e7 |
8 | |
7ed7eba7 |
9 | our $VERSION = '1.003002'; |
c334a5c1 |
10 | $VERSION = eval $VERSION; |
11 | |
ab3370e7 |
12 | our %INFO; |
13 | our %APPLIED_TO; |
1947330a |
14 | our %COMPOSED; |
836aea1b |
15 | our %COMPOSITE_INFO; |
ab3370e7 |
16 | |
cf62c989 |
17 | # Module state workaround totally stolen from Zefram's Module::Runtime. |
18 | |
19 | BEGIN { |
20 | *_WORK_AROUND_BROKEN_MODULE_STATE = "$]" < 5.009 ? sub(){1} : sub(){0}; |
1c93a643 |
21 | *_MRO_MODULE = "$]" < 5.010 ? sub(){"MRO/Compat.pm"} : sub(){"mro.pm"}; |
cf62c989 |
22 | } |
23 | |
24 | sub Role::Tiny::__GUARD__::DESTROY { |
25 | delete $INC{$_[0]->[0]} if @{$_[0]}; |
26 | } |
5e03b55c |
27 | |
fb5074f6 |
28 | sub _load_module { |
fb5074f6 |
29 | (my $proto = $_[0]) =~ s/::/\//g; |
cf62c989 |
30 | $proto .= '.pm'; |
31 | return 1 if $INC{$proto}; |
5e03b55c |
32 | # can't just ->can('can') because a sub-package Foo::Bar::Baz |
33 | # creates a 'Baz::' key in Foo::Bar's symbol table |
34 | return 1 if grep !/::$/, keys %{_getstash($_[0])||{}}; |
cf62c989 |
35 | my $guard = _WORK_AROUND_BROKEN_MODULE_STATE |
36 | && bless([ $proto ], 'Role::Tiny::__GUARD__'); |
37 | require $proto; |
38 | pop @$guard if _WORK_AROUND_BROKEN_MODULE_STATE; |
fb5074f6 |
39 | return 1; |
40 | } |
41 | |
ab3370e7 |
42 | sub import { |
43 | my $target = caller; |
a1164a0b |
44 | my $me = shift; |
3ca7ff56 |
45 | strict->import; |
46 | warnings->import(FATAL => 'all'); |
85b8e25f |
47 | return if $me->is_role($target); # already exported into this package |
8e8b04eb |
48 | $INFO{$target}{is_role} = 1; |
ab3370e7 |
49 | # get symbol table reference |
76acfa6c |
50 | my $stash = _getstash($target); |
ab3370e7 |
51 | # install before/after/around subs |
52 | foreach my $type (qw(before after around)) { |
5a247406 |
53 | *{_getglob "${target}::${type}"} = sub { |
7568ba55 |
54 | require Class::Method::Modifiers; |
ab3370e7 |
55 | push @{$INFO{$target}{modifiers}||=[]}, [ $type => @_ ]; |
ef2da711 |
56 | return; |
ab3370e7 |
57 | }; |
58 | } |
5a247406 |
59 | *{_getglob "${target}::requires"} = sub { |
ab3370e7 |
60 | push @{$INFO{$target}{requires}||=[]}, @_; |
ef2da711 |
61 | return; |
ab3370e7 |
62 | }; |
5a247406 |
63 | *{_getglob "${target}::with"} = sub { |
836aea1b |
64 | $me->apply_roles_to_package($target, @_); |
ef2da711 |
65 | return; |
96d3f07a |
66 | }; |
7b8177f8 |
67 | # grab all *non-constant* (stash slot is not a scalarref) subs present |
ab3370e7 |
68 | # in the symbol table and store their refaddrs (no need to forcibly |
f1ce2b19 |
69 | # inflate constant subs into real subs) with a map to the coderefs in |
70 | # case of copying or re-use |
71 | my @not_methods = (map { *$_{CODE}||() } grep !ref($_), values %$stash); |
12f8eb0b |
72 | @{$INFO{$target}{not_methods}={}}{@not_methods} = @not_methods; |
ab3370e7 |
73 | # a role does itself |
74 | $APPLIED_TO{$target} = { $target => undef }; |
75 | } |
76 | |
3d203c73 |
77 | sub role_application_steps { |
c0978659 |
78 | qw(_install_methods _check_requires _install_modifiers _copy_applied_list); |
3d203c73 |
79 | } |
80 | |
836aea1b |
81 | sub apply_single_role_to_package { |
369a4c50 |
82 | my ($me, $to, $role) = @_; |
1947330a |
83 | |
fb5074f6 |
84 | _load_module($role); |
85 | |
ab3370e7 |
86 | die "This is apply_role_to_package" if ref($to); |
85b8e25f |
87 | die "${role} is not a Role::Tiny" unless $me->is_role($role); |
1947330a |
88 | |
3d203c73 |
89 | foreach my $step ($me->role_application_steps) { |
90 | $me->$step($to, $role); |
91 | } |
92 | } |
1947330a |
93 | |
3d203c73 |
94 | sub _copy_applied_list { |
95 | my ($me, $to, $role) = @_; |
1947330a |
96 | # copy our role list into the target's |
97 | @{$APPLIED_TO{$to}||={}}{keys %{$APPLIED_TO{$role}}} = (); |
98 | } |
99 | |
100 | sub apply_roles_to_object { |
101 | my ($me, $object, @roles) = @_; |
102 | die "No roles supplied!" unless @roles; |
103 | my $class = ref($object); |
3f9b723f |
104 | # on perl < 5.8.9, magic isn't copied to all ref copies. bless the parameter |
105 | # directly, so at least the variable passed to us will get any magic applied |
106 | bless($_[1], $me->create_class_with_roles($class, @roles)); |
1947330a |
107 | } |
108 | |
bc615098 |
109 | my $role_suffix = 'A000'; |
110 | sub _composite_name { |
111 | my ($me, $superclass, @roles) = @_; |
112 | |
113 | my $new_name = join( |
114 | '__WITH__', $superclass, my $compose_name = join '__AND__', @roles |
115 | ); |
116 | |
117 | if (length($new_name) > 252) { |
118 | $new_name = $COMPOSED{abbrev}{$new_name} |
119 | ||= substr($new_name, 0, 250 - length $role_suffix).'__'.$role_suffix++; |
120 | } |
121 | return wantarray ? ($new_name, $compose_name) : $new_name; |
122 | } |
123 | |
1947330a |
124 | sub create_class_with_roles { |
125 | my ($me, $superclass, @roles) = @_; |
126 | |
fb5074f6 |
127 | die "No roles supplied!" unless @roles; |
128 | |
f52b9821 |
129 | _load_module($superclass); |
2c580674 |
130 | { |
131 | my %seen; |
132 | $seen{$_}++ for @roles; |
133 | if (my @dupes = grep $seen{$_} > 1, @roles) { |
134 | die "Duplicated roles: ".join(', ', @dupes); |
135 | } |
136 | } |
137 | |
bc615098 |
138 | my ($new_name, $compose_name) = $me->_composite_name($superclass, @roles); |
c69190f1 |
139 | |
1947330a |
140 | return $new_name if $COMPOSED{class}{$new_name}; |
141 | |
142 | foreach my $role (@roles) { |
fb5074f6 |
143 | _load_module($role); |
85b8e25f |
144 | die "${role} is not a Role::Tiny" unless $me->is_role($role); |
1947330a |
145 | } |
146 | |
1c93a643 |
147 | require(_MRO_MODULE); |
1947330a |
148 | |
1ad34a6f |
149 | my $composite_info = $me->_composite_info_for(@roles); |
150 | my %conflicts = %{$composite_info->{conflicts}}; |
471419f6 |
151 | if (keys %conflicts) { |
afc827be |
152 | my $fail = |
471419f6 |
153 | join "\n", |
154 | map { |
155 | "Method name conflict for '$_' between roles " |
156 | ."'".join(' and ', sort values %{$conflicts{$_}})."'" |
157 | .", cannot apply these simultaneously to an object." |
158 | } keys %conflicts; |
159 | die $fail; |
160 | } |
161 | |
76b67e35 |
162 | my @composable = map $me->_composable_package_for($_), reverse @roles; |
163 | |
164 | # some methods may not exist in the role, but get generated by |
165 | # _composable_package_for (Moose accessors via Moo). filter out anything |
166 | # provided by the composable packages, excluding the subs we generated to |
167 | # make modifiers work. |
1ad34a6f |
168 | my @requires = grep { |
169 | my $method = $_; |
170 | !grep $_->can($method) && !$COMPOSED{role}{$_}{modifiers_only}{$method}, |
171 | @composable |
172 | } @{$composite_info->{requires}}; |
1947330a |
173 | |
174 | $me->_check_requires( |
1ad34a6f |
175 | $superclass, $compose_name, \@requires |
1947330a |
176 | ); |
1947330a |
177 | |
1ad34a6f |
178 | *{_getglob("${new_name}::ISA")} = [ @composable, $superclass ]; |
179 | |
1947330a |
180 | @{$APPLIED_TO{$new_name}||={}}{ |
91037640 |
181 | map keys %{$APPLIED_TO{$_}}, @roles |
1947330a |
182 | } = (); |
183 | |
184 | $COMPOSED{class}{$new_name} = 1; |
185 | return $new_name; |
186 | } |
187 | |
92e1c5f8 |
188 | # preserved for compat, and apply_roles_to_package calls it to allow an |
189 | # updated Role::Tiny to use a non-updated Moo::Role |
190 | |
836aea1b |
191 | sub apply_role_to_package { shift->apply_single_role_to_package(@_) } |
192 | |
193 | sub apply_roles_to_package { |
60dfe768 |
194 | my ($me, $to, @roles) = @_; |
195 | |
92e1c5f8 |
196 | return $me->apply_role_to_package($to, $roles[0]) if @roles == 1; |
60dfe768 |
197 | |
836aea1b |
198 | my %conflicts = %{$me->_composite_info_for(@roles)->{conflicts}}; |
032cad56 |
199 | my @have = grep $to->can($_), keys %conflicts; |
200 | delete @conflicts{@have}; |
201 | |
671b5a88 |
202 | if (keys %conflicts) { |
afc827be |
203 | my $fail = |
60dfe768 |
204 | join "\n", |
671b5a88 |
205 | map { |
206 | "Due to a method name conflict between roles " |
207 | ."'".join(' and ', sort values %{$conflicts{$_}})."'" |
208 | .", the method '$_' must be implemented by '${to}'" |
209 | } keys %conflicts; |
210 | die $fail; |
60dfe768 |
211 | } |
06b06c8e |
212 | |
032cad56 |
213 | # conflicting methods are supposed to be treated as required by the |
214 | # composed role. we don't have an actual composed role, but because |
215 | # we know the target class already provides them, we can instead |
216 | # pretend that the roles don't do for the duration of application. |
217 | my @role_methods = map $me->_concrete_methods_of($_), @roles; |
218 | # separate loops, since local ..., delete ... for ...; creates a scope |
219 | local @{$_}{@have} for @role_methods; |
220 | delete @{$_}{@have} for @role_methods; |
221 | |
06b06c8e |
222 | # the if guard here is essential since otherwise we accidentally create |
223 | # a $INFO for something that isn't a Role::Tiny (or Moo::Role) because |
224 | # autovivification hates us and wants us to die() |
225 | if ($INFO{$to}) { |
226 | delete $INFO{$to}{methods}; # reset since we're about to add methods |
227 | } |
2603ea93 |
228 | |
229 | # backcompat: allow subclasses to use apply_single_role_to_package |
230 | # to apply changes. set a local var so ours does nothing. |
231 | our %BACKCOMPAT_HACK; |
232 | if($me ne __PACKAGE__ |
233 | and exists $BACKCOMPAT_HACK{$me} ? $BACKCOMPAT_HACK{$me} : |
234 | $BACKCOMPAT_HACK{$me} = |
235 | $me->can('role_application_steps') |
236 | == \&role_application_steps |
237 | && $me->can('apply_single_role_to_package') |
238 | != \&apply_single_role_to_package |
239 | ) { |
3d203c73 |
240 | foreach my $role (@roles) { |
2603ea93 |
241 | $me->apply_single_role_to_package($to, $role); |
3d203c73 |
242 | } |
667f4e70 |
243 | } |
113e7b42 |
244 | else { |
245 | foreach my $step ($me->role_application_steps) { |
246 | foreach my $role (@roles) { |
247 | $me->$step($to, $role); |
248 | } |
2ff0bab0 |
249 | } |
ee81e663 |
250 | } |
671b5a88 |
251 | $APPLIED_TO{$to}{join('|',@roles)} = 1; |
252 | } |
253 | |
836aea1b |
254 | sub _composite_info_for { |
671b5a88 |
255 | my ($me, @roles) = @_; |
836aea1b |
256 | $COMPOSITE_INFO{join('|', sort @roles)} ||= do { |
667f4e70 |
257 | foreach my $role (@roles) { |
258 | _load_module($role); |
259 | } |
671b5a88 |
260 | my %methods; |
261 | foreach my $role (@roles) { |
262 | my $this_methods = $me->_concrete_methods_of($role); |
263 | $methods{$_}{$this_methods->{$_}} = $role for keys %$this_methods; |
264 | } |
1ad34a6f |
265 | my %requires; |
266 | @requires{map @{$INFO{$_}{requires}||[]}, @roles} = (); |
267 | delete $requires{$_} for keys %methods; |
671b5a88 |
268 | delete $methods{$_} for grep keys(%{$methods{$_}}) == 1, keys %methods; |
1ad34a6f |
269 | +{ conflicts => \%methods, requires => [keys %requires] } |
671b5a88 |
270 | }; |
60dfe768 |
271 | } |
272 | |
1947330a |
273 | sub _composable_package_for { |
274 | my ($me, $role) = @_; |
275 | my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role; |
276 | return $composed_name if $COMPOSED{role}{$composed_name}; |
277 | $me->_install_methods($composed_name, $role); |
278 | my $base_name = $composed_name.'::_BASE'; |
76b67e35 |
279 | # force stash to exist so ->can doesn't complain |
1ad34a6f |
280 | _getstash($base_name); |
c49b0f72 |
281 | # Not using _getglob, since setting @ISA via the typeglob breaks |
282 | # inheritance on 5.10.0 if the stash has previously been accessed an |
283 | # then a method called on the class (in that order!), which |
284 | # ->_install_methods (with the help of ->_install_does) ends up doing. |
285 | { no strict 'refs'; @{"${composed_name}::ISA"} = ( $base_name ); } |
1947330a |
286 | my $modifiers = $INFO{$role}{modifiers}||[]; |
b1eebd55 |
287 | my @mod_base; |
1ad34a6f |
288 | my @modifiers = grep !$composed_name->can($_), |
289 | do { my %h; @h{map @{$_}[1..$#$_-1], @$modifiers} = (); keys %h }; |
290 | foreach my $modified (@modifiers) { |
b1eebd55 |
291 | push @mod_base, "sub ${modified} { shift->next::method(\@_) }"; |
1947330a |
292 | } |
385f5087 |
293 | my $e; |
59812c87 |
294 | { |
295 | local $@; |
296 | eval(my $code = join "\n", "package ${base_name};", @mod_base); |
385f5087 |
297 | $e = "Evaling failed: $@\nTrying to eval:\n${code}" if $@; |
59812c87 |
298 | } |
385f5087 |
299 | die $e if $e; |
3d203c73 |
300 | $me->_install_modifiers($composed_name, $role); |
1ad34a6f |
301 | $COMPOSED{role}{$composed_name} = { |
302 | modifiers_only => { map { $_ => 1 } @modifiers }, |
303 | }; |
1947330a |
304 | return $composed_name; |
305 | } |
306 | |
307 | sub _check_requires { |
1ad34a6f |
308 | my ($me, $to, $name, $requires) = @_; |
309 | return unless my @requires = @{$requires||$INFO{$name}{requires}||[]}; |
1947330a |
310 | if (my @requires_fail = grep !$to->can($_), @requires) { |
311 | # role -> role, add to requires, role -> class, error out |
312 | if (my $to_info = $INFO{$to}) { |
313 | push @{$to_info->{requires}||=[]}, @requires_fail; |
314 | } else { |
315 | die "Can't apply ${name} to ${to} - missing ".join(', ', @requires_fail); |
316 | } |
317 | } |
318 | } |
319 | |
4db3a740 |
320 | sub _concrete_methods_of { |
321 | my ($me, $role) = @_; |
1947330a |
322 | my $info = $INFO{$role}; |
60dfe768 |
323 | # grab role symbol table |
76acfa6c |
324 | my $stash = _getstash($role); |
12f8eb0b |
325 | # reverse so our keys become the values (captured coderefs) in case |
326 | # they got copied or re-used since |
123a5a6e |
327 | my $not_methods = { reverse %{$info->{not_methods}||{}} }; |
eb8fcb2d |
328 | $info->{methods} ||= +{ |
60dfe768 |
329 | # grab all code entries that aren't in the not_methods list |
330 | map { |
331 | my $code = *{$stash->{$_}}{CODE}; |
f1ce2b19 |
332 | ( ! $code or exists $not_methods->{$code} ) ? () : ($_ => $code) |
60dfe768 |
333 | } grep !ref($stash->{$_}), keys %$stash |
ab3370e7 |
334 | }; |
4db3a740 |
335 | } |
336 | |
337 | sub methods_provided_by { |
338 | my ($me, $role) = @_; |
85b8e25f |
339 | die "${role} is not a Role::Tiny" unless $me->is_role($role); |
340 | (keys %{$me->_concrete_methods_of($role)}, @{$INFO{$role}->{requires}||[]}); |
4db3a740 |
341 | } |
342 | |
343 | sub _install_methods { |
344 | my ($me, $to, $role) = @_; |
345 | |
346 | my $info = $INFO{$role}; |
347 | |
348 | my $methods = $me->_concrete_methods_of($role); |
1947330a |
349 | |
ab3370e7 |
350 | # grab target symbol table |
76acfa6c |
351 | my $stash = _getstash($to); |
1947330a |
352 | |
ab3370e7 |
353 | # determine already extant methods of target |
354 | my %has_methods; |
355 | @has_methods{grep |
faa9ce11 |
356 | +(ref($stash->{$_}) || *{$stash->{$_}}{CODE}), |
ab3370e7 |
357 | keys %$stash |
358 | } = (); |
ab3370e7 |
359 | |
1947330a |
360 | foreach my $i (grep !exists $has_methods{$_}, keys %$methods) { |
ab3370e7 |
361 | no warnings 'once'; |
9256ec21 |
362 | my $glob = _getglob "${to}::${i}"; |
363 | *$glob = $methods->{$i}; |
364 | |
365 | # overloads using method names have the method stored in the scalar slot |
3f9b723f |
366 | # and &overload::nil in the code slot. |
9256ec21 |
367 | next |
368 | unless $i =~ /^\(/ |
369 | && defined &overload::nil |
370 | && $methods->{$i} == \&overload::nil; |
371 | |
372 | my $overload = ${ *{_getglob "${role}::${i}"}{SCALAR} }; |
373 | next |
374 | unless defined $overload; |
375 | |
376 | *$glob = \$overload; |
ab3370e7 |
377 | } |
1133b2a9 |
378 | |
fa89d582 |
379 | $me->_install_does($to); |
1947330a |
380 | } |
ab3370e7 |
381 | |
1947330a |
382 | sub _install_modifiers { |
3d203c73 |
383 | my ($me, $to, $name) = @_; |
384 | return unless my $modifiers = $INFO{$name}{modifiers}; |
dccea57d |
385 | if (my $info = $INFO{$to}) { |
386 | push @{$info->{modifiers}}, @{$modifiers||[]}; |
387 | } else { |
388 | foreach my $modifier (@{$modifiers||[]}) { |
389 | $me->_install_single_modifier($to, @$modifier); |
390 | } |
96d3f07a |
391 | } |
ab3370e7 |
392 | } |
393 | |
3117a19e |
394 | my $vcheck_error; |
395 | |
dccea57d |
396 | sub _install_single_modifier { |
397 | my ($me, @args) = @_; |
3117a19e |
398 | defined($vcheck_error) or $vcheck_error = do { |
399 | local $@; |
400 | eval { Class::Method::Modifiers->VERSION(1.05); 1 } |
401 | ? 0 |
402 | : $@ |
403 | }; |
404 | $vcheck_error and die $vcheck_error; |
dccea57d |
405 | Class::Method::Modifiers::install_modifier(@args); |
406 | } |
407 | |
fa89d582 |
408 | my $FALLBACK = sub { 0 }; |
409 | sub _install_does { |
410 | my ($me, $to) = @_; |
56efcee2 |
411 | |
fa89d582 |
412 | # only add does() method to classes |
85b8e25f |
413 | return if $me->is_role($to); |
56efcee2 |
414 | |
fa89d582 |
415 | # add does() only if they don't have one |
416 | *{_getglob "${to}::does"} = \&does_role unless $to->can('does'); |
56efcee2 |
417 | |
418 | return |
419 | if $to->can('DOES') and $to->can('DOES') != (UNIVERSAL->can('DOES') || 0); |
420 | |
fa89d582 |
421 | my $existing = $to->can('DOES') || $to->can('isa') || $FALLBACK; |
422 | my $new_sub = sub { |
423 | my ($proto, $role) = @_; |
424 | Role::Tiny::does_role($proto, $role) or $proto->$existing($role); |
425 | }; |
426 | no warnings 'redefine'; |
427 | *{_getglob "${to}::DOES"} = $new_sub; |
428 | } |
429 | |
ab3370e7 |
430 | sub does_role { |
390ac406 |
431 | my ($proto, $role) = @_; |
1c93a643 |
432 | require(_MRO_MODULE); |
91037640 |
433 | foreach my $class (@{mro::get_linear_isa(ref($proto)||$proto)}) { |
434 | return 1 if exists $APPLIED_TO{$class}{$role}; |
435 | } |
436 | return 0; |
ab3370e7 |
437 | } |
438 | |
fe2e95b1 |
439 | sub is_role { |
440 | my ($me, $role) = @_; |
daee1474 |
441 | return !!($INFO{$role} && $INFO{$role}{is_role}); |
fe2e95b1 |
442 | } |
443 | |
ab3370e7 |
444 | 1; |
afc827be |
445 | __END__ |
5febcf4d |
446 | |
7ce64c71 |
447 | =encoding utf-8 |
448 | |
0b6e5fff |
449 | =head1 NAME |
450 | |
70061353 |
451 | Role::Tiny - Roles. Like a nouvelle cuisine portion size slice of Moose. |
5febcf4d |
452 | |
453 | =head1 SYNOPSIS |
454 | |
455 | package Some::Role; |
456 | |
457 | use Role::Tiny; |
458 | |
459 | sub foo { ... } |
460 | |
461 | sub bar { ... } |
462 | |
adcb6c3d |
463 | around baz => sub { ... } |
464 | |
5febcf4d |
465 | 1; |
466 | |
467 | else where |
468 | |
469 | package Some::Class; |
470 | |
a1164a0b |
471 | use Role::Tiny::With; |
5febcf4d |
472 | |
473 | # bar gets imported, but not foo |
a1164a0b |
474 | with 'Some::Role'; |
5febcf4d |
475 | |
476 | sub foo { ... } |
477 | |
adcb6c3d |
478 | # baz is wrapped in the around modifier by Class::Method::Modifiers |
479 | sub baz { ... } |
480 | |
5febcf4d |
481 | 1; |
482 | |
adcb6c3d |
483 | If you wanted attributes as well, look at L<Moo::Role>. |
484 | |
5febcf4d |
485 | =head1 DESCRIPTION |
486 | |
487 | C<Role::Tiny> is a minimalist role composition tool. |
488 | |
489 | =head1 ROLE COMPOSITION |
490 | |
491 | Role composition can be thought of as much more clever and meaningful multiple |
492 | inheritance. The basics of this implementation of roles is: |
493 | |
494 | =over 2 |
495 | |
496 | =item * |
497 | |
498 | If a method is already defined on a class, that method will not be composed in |
499 | from the role. |
500 | |
501 | =item * |
502 | |
503 | If a method that the role L</requires> to be implemented is not implemented, |
504 | role application will fail loudly. |
505 | |
0d39f9d3 |
506 | =back |
507 | |
5febcf4d |
508 | Unlike L<Class::C3>, where the B<last> class inherited from "wins," role |
836aea1b |
509 | composition is the other way around, where the class wins. If multiple roles |
510 | are applied in a single call (single with statement), then if any of their |
511 | provided methods clash, an exception is raised unless the class provides |
512 | a method since this conflict indicates a potential problem. |
5febcf4d |
513 | |
5febcf4d |
514 | =head1 IMPORTED SUBROUTINES |
515 | |
516 | =head2 requires |
517 | |
518 | requires qw(foo bar); |
519 | |
520 | Declares a list of methods that must be defined to compose role. |
521 | |
522 | =head2 with |
523 | |
524 | with 'Some::Role1'; |
836aea1b |
525 | |
526 | with 'Some::Role1', 'Some::Role2'; |
527 | |
528 | Composes another role into the current role (or class via L<Role::Tiny::With>). |
529 | |
530 | If you have conflicts and want to resolve them in favour of Some::Role1 you |
afc827be |
531 | can instead write: |
836aea1b |
532 | |
533 | with 'Some::Role1'; |
5febcf4d |
534 | with 'Some::Role2'; |
535 | |
836aea1b |
536 | If you have conflicts and want to resolve different conflicts in favour of |
537 | different roles, please refactor your codebase. |
5febcf4d |
538 | |
539 | =head2 before |
540 | |
541 | before foo => sub { ... }; |
542 | |
543 | See L<< Class::Method::Modifiers/before method(s) => sub { ... } >> for full |
544 | documentation. |
545 | |
adcb6c3d |
546 | Note that since you are not required to use method modifiers, |
547 | L<Class::Method::Modifiers> is lazily loaded and we do not declare it as |
548 | a dependency. If your L<Role::Tiny> role uses modifiers you must depend on |
549 | both L<Class::Method::Modifiers> and L<Role::Tiny>. |
550 | |
5febcf4d |
551 | =head2 around |
552 | |
553 | around foo => sub { ... }; |
554 | |
555 | See L<< Class::Method::Modifiers/around method(s) => sub { ... } >> for full |
556 | documentation. |
557 | |
adcb6c3d |
558 | Note that since you are not required to use method modifiers, |
559 | L<Class::Method::Modifiers> is lazily loaded and we do not declare it as |
560 | a dependency. If your L<Role::Tiny> role uses modifiers you must depend on |
561 | both L<Class::Method::Modifiers> and L<Role::Tiny>. |
562 | |
5febcf4d |
563 | =head2 after |
564 | |
565 | after foo => sub { ... }; |
566 | |
567 | See L<< Class::Method::Modifiers/after method(s) => sub { ... } >> for full |
568 | documentation. |
569 | |
adcb6c3d |
570 | Note that since you are not required to use method modifiers, |
571 | L<Class::Method::Modifiers> is lazily loaded and we do not declare it as |
572 | a dependency. If your L<Role::Tiny> role uses modifiers you must depend on |
573 | both L<Class::Method::Modifiers> and L<Role::Tiny>. |
574 | |
26b8d929 |
575 | =head2 Strict and Warnings |
576 | |
577 | In addition to importing subroutines, using C<Role::Tiny> applies L<strict> and |
578 | L<fatal warnings|perllexwarn/Fatal Warnings> to the caller. It's possible to |
579 | disable these if desired: |
580 | |
581 | use Role::Tiny; |
582 | use warnings NONFATAL => 'all'; |
583 | |
ec28e16b |
584 | =head1 SUBROUTINES |
585 | |
586 | =head2 does_role |
587 | |
588 | if (Role::Tiny::does_role($foo, 'Some::Role')) { |
589 | ... |
590 | } |
591 | |
592 | Returns true if class has been composed with role. |
593 | |
594 | This subroutine is also installed as ->does on any class a Role::Tiny is |
595 | composed into unless that class already has an ->does method, so |
596 | |
597 | if ($foo->does('Some::Role')) { |
598 | ... |
599 | } |
600 | |
fa89d582 |
601 | will work for classes but to test a role, one must use ::does_role directly. |
602 | |
603 | Additionally, Role::Tiny will override the standard Perl C<DOES> method |
604 | for your class. However, if C<any> class in your class' inheritance |
8159d057 |
605 | hierarchy provides C<DOES>, then Role::Tiny will not override it. |
ec28e16b |
606 | |
607 | =head1 METHODS |
608 | |
609 | =head2 apply_roles_to_package |
610 | |
611 | Role::Tiny->apply_roles_to_package( |
612 | 'Some::Package', 'Some::Role', 'Some::Other::Role' |
613 | ); |
614 | |
615 | Composes role with package. See also L<Role::Tiny::With>. |
616 | |
617 | =head2 apply_roles_to_object |
618 | |
619 | Role::Tiny->apply_roles_to_object($foo, qw(Some::Role1 Some::Role2)); |
620 | |
621 | Composes roles in order into object directly. Object is reblessed into the |
622 | resulting class. |
623 | |
624 | =head2 create_class_with_roles |
625 | |
626 | Role::Tiny->create_class_with_roles('Some::Base', qw(Some::Role1 Some::Role2)); |
627 | |
628 | Creates a new class based on base, with the roles composed into it in order. |
629 | New class is returned. |
630 | |
fe2e95b1 |
631 | =head2 is_role |
632 | |
633 | Role::Tiny->is_role('Some::Role1') |
634 | |
635 | Returns true if the given package is a role. |
636 | |
3f9b723f |
637 | =head1 CAVEATS |
638 | |
639 | =over 4 |
640 | |
641 | =item * On perl 5.8.8 and earlier, applying a role to an object won't apply any |
642 | overloads from the role to all copies of the object. |
643 | |
adcb6c3d |
644 | =head1 SEE ALSO |
645 | |
646 | L<Role::Tiny> is the attribute-less subset of L<Moo::Role>; L<Moo::Role> is |
647 | a meta-protocol-less subset of the king of role systems, L<Moose::Role>. |
648 | |
ed1d913d |
649 | Ovid's L<Role::Basic> provides roles with a similar scope, but without method |
650 | modifiers, and having some extra usage restrictions. |
adcb6c3d |
651 | |
c334a5c1 |
652 | =head1 AUTHOR |
653 | |
654 | mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk> |
655 | |
656 | =head1 CONTRIBUTORS |
657 | |
658 | dg - David Leadbeater (cpan:DGL) <dgl@dgl.cx> |
659 | |
660 | frew - Arthur Axel "fREW" Schmidt (cpan:FREW) <frioux@gmail.com> |
661 | |
662 | hobbs - Andrew Rodland (cpan:ARODLAND) <arodland@cpan.org> |
663 | |
664 | jnap - John Napiorkowski (cpan:JJNAPIORK) <jjn1056@yahoo.com> |
665 | |
666 | ribasushi - Peter Rabbitson (cpan:RIBASUSHI) <ribasushi@cpan.org> |
667 | |
668 | chip - Chip Salzenberg (cpan:CHIPS) <chip@pobox.com> |
669 | |
670 | ajgb - Alex J. G. Burzyński (cpan:AJGB) <ajgb@cpan.org> |
671 | |
672 | doy - Jesse Luehrs (cpan:DOY) <doy at tozt dot net> |
673 | |
674 | perigrin - Chris Prather (cpan:PERIGRIN) <chris@prather.org> |
675 | |
ec28e16b |
676 | Mithaldu - Christian Walde (cpan:MITHALDU) <walde.christian@googlemail.com> |
677 | |
91037640 |
678 | ilmari - Dagfinn Ilmari Mannsåker (cpan:ILMARI) <ilmari@ilmari.org> |
679 | |
fa89d582 |
680 | tobyink - Toby Inkster (cpan:TOBYINK) <tobyink@cpan.org> |
681 | |
fd64ff80 |
682 | haarg - Graham Knop (cpan:HAARG) <haarg@haarg.org> |
683 | |
c334a5c1 |
684 | =head1 COPYRIGHT |
40f3e3aa |
685 | |
c334a5c1 |
686 | Copyright (c) 2010-2012 the Role::Tiny L</AUTHOR> and L</CONTRIBUTORS> |
687 | as listed above. |
40f3e3aa |
688 | |
c334a5c1 |
689 | =head1 LICENSE |
40f3e3aa |
690 | |
c334a5c1 |
691 | This library is free software and may be distributed under the same terms |
692 | as perl itself. |
40f3e3aa |
693 | |
694 | =cut |