6 use Carp 'confess', 'croak';
7 use Scalar::Util 'blessed';
12 our @EXPORT = qw(before after around super override inner augment has extends with requires excludes confess blessed);
13 our %is_removable = map{ $_ => undef } @EXPORT;
14 delete $is_removable{confess};
15 delete $is_removable{blessed};
18 my $meta = Mouse::Meta::Role->initialize(caller);
22 $meta->add_before_method_modifier($_ => $code);
27 my $meta = Mouse::Meta::Role->initialize(caller);
31 $meta->add_after_method_modifier($_ => $code);
36 my $meta = Mouse::Meta::Role->initialize(caller);
40 $meta->add_around_method_modifier($_ => $code);
46 return unless $Mouse::SUPER_BODY;
47 $Mouse::SUPER_BODY->(@Mouse::SUPER_ARGS);
51 my $classname = caller;
52 my $meta = Mouse::Meta::Role->initialize($classname);
56 my $fullname = "${classname}::${name}";
59 && confess "Cannot add an override of method '$fullname' " .
60 "because there is a local version of '$fullname'";
62 $meta->add_override_method_modifier($name => sub {
63 local $Mouse::SUPER_PACKAGE = shift;
64 local $Mouse::SUPER_BODY = shift;
65 local @Mouse::SUPER_ARGS = @_;
71 # We keep the same errors messages as Moose::Role emits, here.
73 croak "Moose::Role cannot support 'inner'";
77 croak "Moose::Role cannot support 'augment'";
81 my $meta = Mouse::Meta::Role->initialize(caller);
86 $meta->add_attribute($name => \%opts);
89 sub extends { confess "Roles do not currently support 'extends'" }
92 my $meta = Mouse::Meta::Role->initialize(caller);
94 my $args = shift || {};
95 confess "Mouse::Role only supports 'with' on individual roles at a time" if @_ || !ref $args;
97 Mouse::load_class($role);
98 $role->meta->apply($meta, %$args);
102 my $meta = Mouse::Meta::Role->initialize(caller);
103 Carp::croak "Must specify at least one method" unless @_;
104 $meta->add_required_methods(@_);
107 sub excludes { confess "Mouse::Role does not currently support 'excludes'" }
117 # we should never export to main
118 if ($caller eq 'main') {
119 warn qq{$class does not export its sugar to the 'main' package.\n};
123 Mouse::Meta::Role->initialize($caller)->add_method(meta => sub {
124 return Mouse::Meta::Role->initialize(ref($_[0]) || $_[0]);
127 Mouse::Role->export_to_level(1, @_);
138 for my $keyword (@EXPORT) {
140 if(exists $is_removable{$keyword}
141 && ($code = $caller->can($keyword))
142 && (Mouse::Util::get_code_info($code))[0] eq __PACKAGE__){
144 delete $stash->{$keyword};
156 Mouse::Role - define a role in Mouse
160 =head2 meta -> Mouse::Meta::Role
162 Returns this role's metaclass instance.
164 =head2 before (method|methods) => Code
166 Sets up a "before" method modifier. See L<Moose/before> or
167 L<Class::Method::Modifiers/before>.
169 =head2 after (method|methods) => Code
171 Sets up an "after" method modifier. See L<Moose/after> or
172 L<Class::Method::Modifiers/after>.
174 =head2 around (method|methods) => Code
176 Sets up an "around" method modifier. See L<Moose/around> or
177 L<Class::Method::Modifiers/around>.
183 Sets up the "super" keyword. See L<Moose/super>.
185 =item B<override ($name, &sub)>
187 Sets up an "override" method modifier. See L<Moose/Role/override>.
191 This is not supported and emits an error. See L<Moose/Role>.
193 =item B<augment ($name, &sub)>
195 This is not supported and emits an error. See L<Moose/Role>.
199 =head2 has (name|names) => parameters
201 Sets up an attribute (or if passed an arrayref of names, multiple attributes) to
202 this role. See L<Mouse/has>.
204 =head2 confess error -> BOOM
206 L<Carp/confess> for your convenience.
208 =head2 blessed value -> ClassName | undef
210 L<Scalar::Util/blessed> for your convenience.
216 Importing Mouse::Role will give you sugar.
220 Please unimport Mouse (C<no Mouse::Role>) so that if someone calls one of the
221 keywords (such as L</has>) it will break loudly instead breaking subtly.