6 use Carp 'confess', 'croak';
7 use Scalar::Util 'blessed';
11 our @EXPORT = qw(before after around super override inner augment has extends with requires excludes confess blessed);
14 my $meta = Mouse::Meta::Role->initialize(caller);
18 $meta->add_before_method_modifier($_ => $code);
23 my $meta = Mouse::Meta::Role->initialize(caller);
27 $meta->add_after_method_modifier($_ => $code);
32 my $meta = Mouse::Meta::Role->initialize(caller);
36 $meta->add_around_method_modifier($_ => $code);
42 return unless $Mouse::SUPER_BODY;
43 $Mouse::SUPER_BODY->(@Mouse::SUPER_ARGS);
47 my $classname = caller;
48 my $meta = Mouse::Meta::Role->initialize($classname);
52 my $fullname = "${classname}::${name}";
55 && confess "Cannot add an override of method '$fullname' " .
56 "because there is a local version of '$fullname'";
58 $meta->add_override_method_modifier($name => sub {
59 local $Mouse::SUPER_PACKAGE = shift;
60 local $Mouse::SUPER_BODY = shift;
61 local @Mouse::SUPER_ARGS = @_;
67 # We keep the same errors messages as Moose::Role emits, here.
69 croak "Moose::Role cannot support 'inner'";
73 croak "Moose::Role cannot support 'augment'";
77 my $meta = Mouse::Meta::Role->initialize(caller);
82 $meta->add_attribute($name => \%opts);
85 sub extends { confess "Roles do not currently support 'extends'" }
88 my $meta = Mouse::Meta::Role->initialize(caller);
90 my $args = shift || {};
91 confess "Mouse::Role only supports 'with' on individual roles at a time" if @_ || !ref $args;
93 Mouse::load_class($role);
94 $role->meta->apply($meta, %$args);
98 my $meta = Mouse::Meta::Role->initialize(caller);
99 Carp::croak "Must specify at least one method" unless @_;
100 $meta->add_required_methods(@_);
103 sub excludes { confess "Mouse::Role does not currently support 'excludes'" }
113 # we should never export to main
114 if ($caller eq 'main') {
115 warn qq{$class does not export its sugar to the 'main' package.\n};
119 my $meta = Mouse::Meta::Role->initialize(caller);
122 no warnings 'redefine';
123 *{$caller.'::meta'} = sub { $meta };
125 Mouse::Role->export_to_level(1, @_);
132 for my $keyword (@EXPORT) {
133 delete ${ $caller . '::' }{$keyword};
143 Mouse::Role - define a role in Mouse
147 =head2 meta -> Mouse::Meta::Role
149 Returns this role's metaclass instance.
151 =head2 before (method|methods) => Code
153 Sets up a "before" method modifier. See L<Moose/before> or
154 L<Class::Method::Modifiers/before>.
156 =head2 after (method|methods) => Code
158 Sets up an "after" method modifier. See L<Moose/after> or
159 L<Class::Method::Modifiers/after>.
161 =head2 around (method|methods) => Code
163 Sets up an "around" method modifier. See L<Moose/around> or
164 L<Class::Method::Modifiers/around>.
168 Sets up the "super" keyword. See L<Moose/super>.
170 =item B<override ($name, &sub)>
172 Sets up an "override" method modifier. See L<Moose/Role/override>.
176 This is not supported and emits an error. See L<Moose/Role>.
178 =item B<augment ($name, &sub)>
180 This is not supported and emits an error. See L<Moose/Role>.
182 =head2 has (name|names) => parameters
184 Sets up an attribute (or if passed an arrayref of names, multiple attributes) to
185 this role. See L<Mouse/has>.
187 =head2 confess error -> BOOM
189 L<Carp/confess> for your convenience.
191 =head2 blessed value -> ClassName | undef
193 L<Scalar::Util/blessed> for your convenience.
199 Importing Mouse::Role will give you sugar.
203 Please unimport Mouse (C<no Mouse::Role>) so that if someone calls one of the
204 keywords (such as L</has>) it will break loudly instead breaking subtly.