7 use Scalar::Util 'blessed';
9 use Mouse::Util qw(load_class not_supported);
24 our %is_removable = map{ $_ => undef } @EXPORT;
25 delete $is_removable{confess};
26 delete $is_removable{blessed};
29 my $meta = Mouse::Meta::Role->initialize(scalar caller);
33 $meta->add_before_method_modifier($_ => $code);
38 my $meta = Mouse::Meta::Role->initialize(scalar caller);
42 $meta->add_after_method_modifier($_ => $code);
47 my $meta = Mouse::Meta::Role->initialize(scalar caller);
51 $meta->add_around_method_modifier($_ => $code);
57 return unless $Mouse::SUPER_BODY;
58 $Mouse::SUPER_BODY->(@Mouse::SUPER_ARGS);
62 my $classname = caller;
63 my $meta = Mouse::Meta::Role->initialize($classname);
67 my $fullname = "${classname}::${name}";
70 && $meta->throw_error("Cannot add an override of method '$fullname' "
71 . "because there is a local version of '$fullname'");
73 $meta->add_override_method_modifier($name => sub {
74 local $Mouse::SUPER_PACKAGE = shift;
75 local $Mouse::SUPER_BODY = shift;
76 local @Mouse::SUPER_ARGS = @_;
82 # We keep the same errors messages as Moose::Role emits, here.
84 Carp::croak "Roles cannot support 'inner'";
88 Carp::croak "Roles cannot support 'augment'";
92 my $meta = Mouse::Meta::Role->initialize(scalar caller);
97 $meta->add_attribute($name => \%opts);
101 Carp::croak "Roles do not support 'extends'"
105 my $meta = Mouse::Meta::Role->initialize(scalar caller);
106 Mouse::Util::apply_all_roles($meta->name, @_);
110 my $meta = Mouse::Meta::Role->initialize(scalar caller);
111 $meta->throw_error("Must specify at least one method") unless @_;
112 $meta->add_required_methods(@_);
127 # we should never export to main
128 if ($caller eq 'main') {
129 warn qq{$class does not export its sugar to the 'main' package.\n};
133 Mouse::Meta::Role->initialize($caller)->add_method(meta => sub {
134 return Mouse::Meta::Role->initialize(ref($_[0]) || $_[0]);
137 Mouse::Role->export_to_level(1, @_);
148 for my $keyword (@EXPORT) {
150 if(exists $is_removable{$keyword}
151 && ($code = $caller->can($keyword))
152 && (Mouse::Util::get_code_info($code))[0] eq __PACKAGE__){
154 delete $stash->{$keyword};
166 Mouse::Role - define a role in Mouse
170 =head2 meta -> Mouse::Meta::Role
172 Returns this role's metaclass instance.
174 =head2 before (method|methods) => Code
176 Sets up a "before" method modifier. See L<Moose/before> or
177 L<Class::Method::Modifiers/before>.
179 =head2 after (method|methods) => Code
181 Sets up an "after" method modifier. See L<Moose/after> or
182 L<Class::Method::Modifiers/after>.
184 =head2 around (method|methods) => Code
186 Sets up an "around" method modifier. See L<Moose/around> or
187 L<Class::Method::Modifiers/around>.
193 Sets up the "super" keyword. See L<Moose/super>.
195 =item B<override ($name, &sub)>
197 Sets up an "override" method modifier. See L<Moose/Role/override>.
201 This is not supported and emits an error. See L<Moose/Role>.
203 =item B<augment ($name, &sub)>
205 This is not supported and emits an error. See L<Moose/Role>.
209 =head2 has (name|names) => parameters
211 Sets up an attribute (or if passed an arrayref of names, multiple attributes) to
212 this role. See L<Mouse/has>.
214 =head2 confess error -> BOOM
216 L<Carp/confess> for your convenience.
218 =head2 blessed value -> ClassName | undef
220 L<Scalar::Util/blessed> for your convenience.
226 Importing Mouse::Role will give you sugar.
230 Please unimport Mouse (C<no Mouse::Role>) so that if someone calls one of the
231 keywords (such as L</has>) it will break loudly instead breaking subtly.