3 sub _getglob { \*{$_[0]} }
4 sub _getstash { \%{"$_[0]::"} }
7 use warnings FATAL => 'all';
9 our $VERSION = '1.000001'; # 1.0.1
10 $VERSION = eval $VERSION;
16 # Module state workaround totally stolen from Zefram's Module::Runtime.
19 *_WORK_AROUND_BROKEN_MODULE_STATE = "$]" < 5.009 ? sub(){1} : sub(){0};
22 sub Role::Tiny::__GUARD__::DESTROY {
23 delete $INC{$_[0]->[0]} if @{$_[0]};
27 (my $proto = $_[0]) =~ s/::/\//g;
29 return 1 if $INC{$proto};
30 # can't just ->can('can') because a sub-package Foo::Bar::Baz
31 # creates a 'Baz::' key in Foo::Bar's symbol table
32 return 1 if grep !/::$/, keys %{_getstash($_[0])||{}};
33 my $guard = _WORK_AROUND_BROKEN_MODULE_STATE
34 && bless([ $proto ], 'Role::Tiny::__GUARD__');
36 pop @$guard if _WORK_AROUND_BROKEN_MODULE_STATE;
44 warnings->import(FATAL => 'all');
45 return if $INFO{$target}; # already exported into this package
46 # get symbol table reference
47 my $stash = do { no strict 'refs'; \%{"${target}::"} };
48 # install before/after/around subs
49 foreach my $type (qw(before after around)) {
50 *{_getglob "${target}::${type}"} = sub {
51 require Class::Method::Modifiers;
52 push @{$INFO{$target}{modifiers}||=[]}, [ $type => @_ ];
55 *{_getglob "${target}::requires"} = sub {
56 push @{$INFO{$target}{requires}||=[]}, @_;
58 *{_getglob "${target}::with"} = sub {
59 $me->apply_union_of_roles_to_package($target, @_);
61 # grab all *non-constant* (stash slot is not a scalarref) subs present
62 # in the symbol table and store their refaddrs (no need to forcibly
63 # inflate constant subs into real subs) - also add '' to here (this
65 @{$INFO{$target}{not_methods}={}}{
66 '', map { *$_{CODE}||() } grep !ref($_), values %$stash
69 $APPLIED_TO{$target} = { $target => undef };
72 sub apply_role_to_package {
73 my ($me, $to, $role) = @_;
77 die "This is apply_role_to_package" if ref($to);
78 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
80 $me->_check_requires($to, $role, @{$info->{requires}||[]});
82 $me->_install_methods($to, $role);
84 $me->_install_modifiers($to, $info->{modifiers});
86 # only add does() method to classes and only if they don't have one
87 if (not $INFO{$to} and not $to->can('does')) {
88 *{_getglob "${to}::does"} = \&does_role;
91 # copy our role list into the target's
92 @{$APPLIED_TO{$to}||={}}{keys %{$APPLIED_TO{$role}}} = ();
95 sub apply_roles_to_object {
96 my ($me, $object, @roles) = @_;
97 die "No roles supplied!" unless @roles;
98 my $class = ref($object);
99 bless($object, $me->create_class_with_roles($class, @roles));
103 sub create_class_with_roles {
104 my ($me, $superclass, @roles) = @_;
106 die "No roles supplied!" unless @roles;
109 '__WITH__', $superclass, my $compose_name = join '__AND__', @roles
112 return $new_name if $COMPOSED{class}{$new_name};
114 foreach my $role (@roles) {
116 die "${role} is not a Role::Tiny" unless $INFO{$role};
125 my @composable = map $me->_composable_package_for($_), reverse @roles;
127 *{_getglob("${new_name}::ISA")} = [ @composable, $superclass ];
129 my @info = map $INFO{$_}, @roles;
131 $me->_check_requires(
132 $new_name, $compose_name,
133 do { my %h; @h{map @{$_->{requires}||[]}, @info} = (); keys %h }
136 *{_getglob "${new_name}::does"} = \&does_role unless $new_name->can('does');
138 @{$APPLIED_TO{$new_name}||={}}{
139 map keys %{$APPLIED_TO{$_}}, @roles
142 $COMPOSED{class}{$new_name} = 1;
146 sub apply_union_of_roles_to_package {
147 my ($me, $to, @roles) = @_;
149 return $me->apply_role_to_package($to, $roles[0]) if @roles == 1;
151 _load_module($_) for @roles;
153 foreach my $role (@roles) {
154 my $this_methods = $me->_concrete_methods_of($role);
155 $methods{$_}{$this_methods->{$_}} = $role for keys %$this_methods;
157 delete $methods{$_} for grep keys(%{$methods{$_}}) == 1, keys %methods;
158 delete $methods{$_} for $me->_concrete_methods_of($to);
162 map "$_ is provided by: ".join(', ', values %{$methods{$_}}),
164 die "Conflict combining ".join(', ', @roles);
166 $me->apply_role_to_package($to, $_) for @roles;
169 sub _composable_package_for {
170 my ($me, $role) = @_;
171 my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role;
172 return $composed_name if $COMPOSED{role}{$composed_name};
173 $me->_install_methods($composed_name, $role);
174 my $base_name = $composed_name.'::_BASE';
175 *{_getglob("${composed_name}::ISA")} = [ $base_name ];
176 my $modifiers = $INFO{$role}{modifiers}||[];
178 foreach my $modified (
179 do { my %h; @h{map $_->[1], @$modifiers} = (); keys %h }
181 push @mod_base, "sub ${modified} { shift->next::method(\@_) }";
186 eval(my $code = join "\n", "package ${base_name};", @mod_base);
187 $e = "Evaling failed: $@\nTrying to eval:\n${code}" if $@;
190 $me->_install_modifiers($composed_name, $modifiers);
191 $COMPOSED{role}{$composed_name} = 1;
192 return $composed_name;
195 sub _check_requires {
196 my ($me, $to, $name, @requires) = @_;
197 if (my @requires_fail = grep !$to->can($_), @requires) {
198 # role -> role, add to requires, role -> class, error out
199 if (my $to_info = $INFO{$to}) {
200 push @{$to_info->{requires}||=[]}, @requires_fail;
202 die "Can't apply ${name} to ${to} - missing ".join(', ', @requires_fail);
207 sub _concrete_methods_of {
208 my ($me, $role) = @_;
209 my $info = $INFO{$role};
210 # grab role symbol table
211 my $stash = do { no strict 'refs'; \%{"${role}::"}};
212 my $not_methods = $info->{not_methods};
214 # grab all code entries that aren't in the not_methods list
216 my $code = *{$stash->{$_}}{CODE};
217 # rely on the '' key we added in import for "no code here"
218 exists $not_methods->{$code||''} ? () : ($_ => $code)
219 } grep !ref($stash->{$_}), keys %$stash
223 sub methods_provided_by {
224 my ($me, $role) = @_;
225 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
226 (keys %{$me->_concrete_methods_of($role)}, @{$info->{requires}||[]});
229 sub _install_methods {
230 my ($me, $to, $role) = @_;
232 my $info = $INFO{$role};
234 my $methods = $me->_concrete_methods_of($role);
236 # grab target symbol table
237 my $stash = do { no strict 'refs'; \%{"${to}::"}};
239 # determine already extant methods of target
242 +(ref($stash->{$_}) || *{$stash->{$_}}{CODE}),
246 foreach my $i (grep !exists $has_methods{$_}, keys %$methods) {
248 *{_getglob "${to}::${i}"} = $methods->{$i};
252 sub _install_modifiers {
253 my ($me, $to, $modifiers) = @_;
254 if (my $info = $INFO{$to}) {
255 push @{$info->{modifiers}}, @{$modifiers||[]};
257 foreach my $modifier (@{$modifiers||[]}) {
258 $me->_install_single_modifier($to, @$modifier);
263 sub _install_single_modifier {
264 my ($me, @args) = @_;
265 Class::Method::Modifiers::install_modifier(@args);
269 my ($proto, $role) = @_;
270 return exists $APPLIED_TO{ref($proto)||$proto}{$role};
277 Role::Tiny - Roles. Like a nouvelle cuisine portion size slice of Moose.
289 around baz => sub { ... }
297 use Role::Tiny::With;
299 # bar gets imported, but not foo
304 # baz is wrapped in the around modifier by Class::Method::Modifiers
309 If you wanted attributes as well, look at L<Moo::Role>.
313 C<Role::Tiny> is a minimalist role composition tool.
315 =head1 ROLE COMPOSITION
317 Role composition can be thought of as much more clever and meaningful multiple
318 inheritance. The basics of this implementation of roles is:
324 If a method is already defined on a class, that method will not be composed in
329 If a method that the role L</requires> to be implemented is not implemented,
330 role application will fail loudly.
334 Unlike L<Class::C3>, where the B<last> class inherited from "wins," role
335 composition is the other way around, where first wins. In a more complete
336 system (see L<Moose>) roles are checked to see if they clash. The goal of this
337 is to be much simpler, hence disallowing composition of multiple roles at once.
341 =head2 apply_role_to_package
343 Role::Tiny->apply_role_to_package('Some::Package', 'Some::Role');
345 Composes role with package. See also L<Role::Tiny::With>.
347 =head2 apply_roles_to_object
349 Role::Tiny->apply_roles_to_object($foo, qw(Some::Role1 Some::Role2));
351 Composes roles in order into object directly. Object is reblessed into the
354 =head2 create_class_with_roles
356 Role::Tiny->create_class_with_roles('Some::Base', qw(Some::Role1 Some::Role2));
358 Creates a new class based on base, with the roles composed into it in order.
359 New class is returned.
365 if (Role::Tiny::does_role($foo, 'Some::Role')) {
369 Returns true if class has been composed with role.
371 This subroutine is also installed as ->does on any class a Role::Tiny is
372 composed into unless that class already has an ->does method, so
374 if ($foo->does('Some::Role')) {
378 will work for classes but to test a role, one must use ::does_role directly
380 =head1 IMPORTED SUBROUTINES
384 requires qw(foo bar);
386 Declares a list of methods that must be defined to compose role.
393 Composes another role into the current role. Only one role may be composed in
394 at a time to allow the code to remain as simple as possible.
398 before foo => sub { ... };
400 See L<< Class::Method::Modifiers/before method(s) => sub { ... } >> for full
403 Note that since you are not required to use method modifiers,
404 L<Class::Method::Modifiers> is lazily loaded and we do not declare it as
405 a dependency. If your L<Role::Tiny> role uses modifiers you must depend on
406 both L<Class::Method::Modifiers> and L<Role::Tiny>.
410 around foo => sub { ... };
412 See L<< Class::Method::Modifiers/around method(s) => sub { ... } >> for full
415 Note that since you are not required to use method modifiers,
416 L<Class::Method::Modifiers> is lazily loaded and we do not declare it as
417 a dependency. If your L<Role::Tiny> role uses modifiers you must depend on
418 both L<Class::Method::Modifiers> and L<Role::Tiny>.
422 after foo => sub { ... };
424 See L<< Class::Method::Modifiers/after method(s) => sub { ... } >> for full
427 Note that since you are not required to use method modifiers,
428 L<Class::Method::Modifiers> is lazily loaded and we do not declare it as
429 a dependency. If your L<Role::Tiny> role uses modifiers you must depend on
430 both L<Class::Method::Modifiers> and L<Role::Tiny>.
434 L<Role::Tiny> is the attribute-less subset of L<Moo::Role>; L<Moo::Role> is
435 a meta-protocol-less subset of the king of role systems, L<Moose::Role>.
437 If you don't want method modifiers and do want to be forcibly restricted
438 to a single role application per class, Ovid's L<Role::Basic> exists. But
439 Stevan Little (the L<Moose> author) and I are both still convinced that
444 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
448 dg - David Leadbeater (cpan:DGL) <dgl@dgl.cx>
450 frew - Arthur Axel "fREW" Schmidt (cpan:FREW) <frioux@gmail.com>
452 hobbs - Andrew Rodland (cpan:ARODLAND) <arodland@cpan.org>
454 jnap - John Napiorkowski (cpan:JJNAPIORK) <jjn1056@yahoo.com>
456 ribasushi - Peter Rabbitson (cpan:RIBASUSHI) <ribasushi@cpan.org>
458 chip - Chip Salzenberg (cpan:CHIPS) <chip@pobox.com>
460 ajgb - Alex J. G. BurzyĆski (cpan:AJGB) <ajgb@cpan.org>
462 doy - Jesse Luehrs (cpan:DOY) <doy at tozt dot net>
464 perigrin - Chris Prather (cpan:PERIGRIN) <chris@prather.org>
468 Copyright (c) 2010-2012 the Role::Tiny L</AUTHOR> and L</CONTRIBUTORS>
473 This library is free software and may be distributed under the same terms