5 use base qw(Role::Tiny);
7 BEGIN { *INFO = \%Role::Tiny::INFO }
14 return if $INFO{$target}; # already exported into this package
15 # get symbol table reference
16 my $stash = do { no strict 'refs'; \%{"${target}::"} };
17 *{_getglob "${target}::has"} = sub {
18 my ($name, %spec) = @_;
19 ($INFO{$target}{accessor_maker} ||= do {
20 require Method::Generate::Accessor;
21 Method::Generate::Accessor->new
22 })->generate_method($target, $name, \%spec);
23 $INFO{$target}{attributes}{$name} = \%spec;
25 goto &Role::Tiny::import;
28 sub apply_role_to_package {
29 my ($me, $to, $role) = @_;
30 $me->SUPER::apply_role_to_package($to, $role);
31 $me->_handle_constructor($to, $INFO{$role}{attributes});
34 sub create_class_with_roles {
35 my ($me, $superclass, @roles) = @_;
38 '__WITH__', $superclass, my $compose_name = join '__AND__', @roles
41 return $new_name if $Role::Tiny::COMPOSED{class}{$new_name};
45 $me->SUPER::create_class_with_roles($superclass, @roles);
47 foreach my $role (@roles) {
48 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
51 $Moo::MAKERS{$new_name} = {};
53 $me->_handle_constructor(
54 $new_name, { map %{$INFO{$_}{attributes}||{}}, @roles }, $superclass
60 sub _install_single_modifier {
62 _install_modifier(@args);
65 sub _handle_constructor {
66 my ($me, $to, $attr_info, $superclass) = @_;
67 return unless $attr_info && keys %$attr_info;
69 @{$INFO{$to}{attributes}||={}}{keys %$attr_info} = values %$attr_info;
71 # only fiddle with the constructor if the target is a Moo class
73 and my $con = Moo->_constructor_maker_for($to, $superclass)) {
74 $con->register_attribute_specs(%$attr_info);
83 Moo::Role - Minimal Object Orientation support for Roles
107 # bar gets imported, but not foo
116 C<Moo::Role> builds upon L<Role::Tiny>, so look there for most of the
117 documentation on how this works. The main addition here is extra bits to make
118 the roles more "Moosey;" which is to say, it adds L</has>.
120 =head1 IMPORTED SUBROUTINES
122 See L<Role::Tiny/IMPORTED SUBROUTINES> for all the other subroutines that are
123 imported by this module.
131 Declares an attribute for the class to be composed into. See
132 L<Moo/has> for all options.
136 See L<Moo> for authors.
138 =head1 COPYRIGHT AND LICENSE
140 See L<Moo> for the copyright and license.