4 use warnings FATAL => 'all';
10 sub _getglob { no strict 'refs'; \*{$_[0]} }
16 # get symbol table reference
17 my $stash = do { no strict 'refs'; \%{"${target}::"} };
18 # install before/after/around subs
19 foreach my $type (qw(before after around)) {
20 *{_getglob "${target}::${type}"} = sub {
21 require Class::Method::Modifiers;
22 push @{$INFO{$target}{modifiers}||=[]}, [ $type => @_ ];
25 *{_getglob "${target}::requires"} = sub {
26 push @{$INFO{$target}{requires}||=[]}, @_;
28 *{_getglob "${target}::with"} = sub {
29 die "Only one role supported at a time by with" if @_ > 1;
30 $me->apply_role_to_package($_[0], $target);
32 # grab all *non-constant* (ref eq 'SCALAR') subs present
33 # in the symbol table and store their refaddrs (no need to forcibly
34 # inflate constant subs into real subs) - also add '' to here (this
36 @{$INFO{$target}{not_methods}={}}{
37 '', map { *$_{CODE}||() } grep !(ref eq 'SCALAR'), values %$stash
40 $APPLIED_TO{$target} = { $target => undef };
43 sub apply_role_to_package {
44 my ($me, $role, $to) = @_;
46 die "This is apply_role_to_package" if ref($to);
47 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
49 $me->_check_requires($to, $role, @{$info->{requires}||[]});
51 $me->_install_methods($to, $role);
53 $me->_install_modifiers($to, $info->{modifiers});
55 # only add does() method to classes and only if they don't have one
56 if (not $INFO{$to} and not $to->can('does')) {
57 *{_getglob "${to}::does"} = \&does_role;
60 # copy our role list into the target's
61 @{$APPLIED_TO{$to}||={}}{keys %{$APPLIED_TO{$role}}} = ();
64 sub apply_roles_to_object {
65 my ($me, $object, @roles) = @_;
66 die "No roles supplied!" unless @roles;
67 my $class = ref($object);
68 bless($object, $me->create_class_with_roles($class, @roles));
72 sub create_class_with_roles {
73 my ($me, $superclass, @roles) = @_;
75 my $new_name = join('+', $superclass, my $compose_name = join '+', @roles);
76 return $new_name if $COMPOSED{class}{$new_name};
78 foreach my $role (@roles) {
79 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
88 my @composable = map $me->_composable_package_for($_), reverse @roles;
90 *{_getglob("${new_name}::ISA")} = [ @composable, $superclass ];
92 my @info = map +($INFO{$_} ? $INFO{$_} : ()), @roles;
95 $new_name, $compose_name,
96 do { my %h; @h{map @{$_->{requires}||[]}, @info} = (); keys %h }
99 *{_getglob "${new_name}::does"} = \&does_role unless $new_name->can('does');
101 @{$APPLIED_TO{$new_name}||={}}{
102 map keys %{$APPLIED_TO{$_}}, @roles
105 $COMPOSED{class}{$new_name} = 1;
109 sub _composable_package_for {
110 my ($me, $role) = @_;
111 my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role;
112 return $composed_name if $COMPOSED{role}{$composed_name};
113 $me->_install_methods($composed_name, $role);
114 my $base_name = $composed_name.'::_BASE';
115 *{_getglob("${composed_name}::ISA")} = [ $base_name ];
116 my $modifiers = $INFO{$role}{modifiers}||[];
118 foreach my $modified (
119 do { my %h; @h{map $_->[1], @$modifiers} = (); keys %h }
121 push @mod_base, "sub ${modified} { shift->next::method(\@_) }";
123 eval(my $code = join "\n", "package ${base_name};", @mod_base);
124 die "Evaling failed: $@\nTrying to eval:\n${code}" if $@;
125 $me->_install_modifiers($composed_name, $modifiers);
126 $COMPOSED{role}{$composed_name} = 1;
127 return $composed_name;
130 sub _check_requires {
131 my ($me, $to, $name, @requires) = @_;
132 if (my @requires_fail = grep !$to->can($_), @requires) {
133 # role -> role, add to requires, role -> class, error out
134 if (my $to_info = $INFO{$to}) {
135 push @{$to_info->{requires}||=[]}, @requires_fail;
137 die "Can't apply ${name} to ${to} - missing ".join(', ', @requires_fail);
142 sub _install_methods {
143 my ($me, $to, $role) = @_;
145 my $info = $INFO{$role};
147 my $methods = $info->{methods} ||= do {
148 # grab role symbol table
149 my $stash = do { no strict 'refs'; \%{"${role}::"}};
150 my $not_methods = $info->{not_methods};
152 # grab all code entries that aren't in the not_methods list
154 my $code = *{$stash->{$_}}{CODE};
155 # rely on the '' key we added in import for "no code here"
156 exists $not_methods->{$code||''} ? () : ($_ => $code)
157 } grep !(ref($stash->{$_}) eq 'SCALAR'), keys %$stash
161 # grab target symbol table
162 my $stash = do { no strict 'refs'; \%{"${to}::"}};
164 # determine already extant methods of target
167 +((ref($stash->{$_}) eq 'SCALAR') || (*{$stash->{$_}}{CODE})),
171 foreach my $i (grep !exists $has_methods{$_}, keys %$methods) {
173 *{_getglob "${to}::${i}"} = $methods->{$i};
177 sub _install_modifiers {
178 my ($me, $to, $modifiers) = @_;
179 foreach my $modifier (@{$modifiers||[]}) {
180 Class::Method::Modifiers::install_modifier($to, @{$modifier});
185 my ($package, $role) = @_;
186 return exists $APPLIED_TO{$package}{$role};