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 if ($INC{'Moo/HandleMoose.pm'}) {
26 Moo::HandleMoose::inject_fake_metaclass_for($target);
28 goto &Role::Tiny::import;
31 sub apply_role_to_package {
32 my ($me, $to, $role) = @_;
33 $me->SUPER::apply_role_to_package($to, $role);
34 $me->_handle_constructor($to, $INFO{$role}{attributes});
37 sub create_class_with_roles {
38 my ($me, $superclass, @roles) = @_;
41 '__WITH__', $superclass, my $compose_name = join '__AND__', @roles
44 return $new_name if $Role::Tiny::COMPOSED{class}{$new_name};
48 $me->SUPER::create_class_with_roles($superclass, @roles);
50 foreach my $role (@roles) {
51 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
54 $Moo::MAKERS{$new_name} = {};
56 $me->_handle_constructor(
57 $new_name, { map %{$INFO{$_}{attributes}||{}}, @roles }, $superclass
63 sub _install_single_modifier {
65 _install_modifier(@args);
68 sub _handle_constructor {
69 my ($me, $to, $attr_info, $superclass) = @_;
70 return unless $attr_info && keys %$attr_info;
72 @{$INFO{$to}{attributes}||={}}{keys %$attr_info} = values %$attr_info;
74 # only fiddle with the constructor if the target is a Moo class
76 and my $con = Moo->_constructor_maker_for($to, $superclass)) {
77 $con->register_attribute_specs(%$attr_info);
86 Moo::Role - Minimal Object Orientation support for Roles
110 # bar gets imported, but not foo
119 C<Moo::Role> builds upon L<Role::Tiny>, so look there for most of the
120 documentation on how this works. The main addition here is extra bits to make
121 the roles more "Moosey;" which is to say, it adds L</has>.
123 =head1 IMPORTED SUBROUTINES
125 See L<Role::Tiny/IMPORTED SUBROUTINES> for all the other subroutines that are
126 imported by this module.
134 Declares an attribute for the class to be composed into. See
135 L<Moo/has> for all options.
139 See L<Moo> for authors.
141 =head1 COPYRIGHT AND LICENSE
143 See L<Moo> for the copyright and license.