We don't need to do both eval and Module::Name -> Module/Name.pm munging
[gitmo/Mouse.git] / lib / Mouse / Role.pm
CommitLineData
f9e68395 1#!/usr/bin/env perl
2package Mouse::Role;
3use strict;
4use warnings;
b32e8fb9 5use base 'Exporter';
f9e68395 6
8da998d9 7use Carp 'confess';
8fcbe7fb 8use Mouse::Util 'blessed';
f9e68395 9
a2227e71 10use Mouse::Meta::Role;
11
b32e8fb9 12our @EXPORT = qw(before after around has extends with requires excludes confess blessed);
13
14sub before {
15 my $meta = Mouse::Meta::Role->initialize(caller);
16
17 my $code = pop;
18 for (@_) {
19 $meta->add_before_method_modifier($_ => $code);
20 }
21}
22
23sub after {
24 my $meta = Mouse::Meta::Role->initialize(caller);
25
26 my $code = pop;
27 for (@_) {
28 $meta->add_after_method_modifier($_ => $code);
f9e68395 29 }
b32e8fb9 30}
31
32sub around {
33 my $meta = Mouse::Meta::Role->initialize(caller);
34
35 my $code = pop;
36 for (@_) {
37 $meta->add_around_method_modifier($_ => $code);
38 }
39}
40
41sub has {
42 my $meta = Mouse::Meta::Role->initialize(caller);
43
44 my $name = shift;
45 my %opts = @_;
46
47 $meta->add_attribute($name => \%opts);
48}
49
50sub extends { confess "Roles do not support 'extends'" }
51
52sub with { confess "Mouse::Role does not currently support 'with'" }
53
2badb84a 54sub requires { confess "Mouse::Role does not currently support 'requires'" }
b32e8fb9 55
2badb84a 56sub excludes { confess "Mouse::Role does not currently support 'excludes'" }
b32e8fb9 57
58sub import {
59 strict->import;
60 warnings->import;
61
62 my $caller = caller;
63 my $meta = Mouse::Meta::Role->initialize(caller);
64
65 no strict 'refs';
66 no warnings 'redefine';
67 *{$caller.'::meta'} = sub { $meta };
68
69 Mouse::Role->export_to_level(1, @_);
70}
f9e68395 71
b32e8fb9 72sub unimport {
73 my $caller = caller;
e71d8033 74
b32e8fb9 75 no strict 'refs';
76 for my $keyword (@EXPORT) {
77 delete ${ $caller . '::' }{$keyword};
f9e68395 78 }
b32e8fb9 79}
f9e68395 80
811;
82
cadd5b5e 83__END__
84
85=head1 NAME
86
87Mouse::Role
88
89=head1 KEYWORDS
90
91=head2 meta -> Mouse::Meta::Role
92
93Returns this role's metaclass instance.
94
95=head2 before (method|methods) => Code
96
97Sets up a "before" method modifier. See L<Moose/before> or
98L<Class::Method::Modifiers/before>.
99
100=head2 after (method|methods) => Code
101
102Sets up an "after" method modifier. See L<Moose/after> or
103L<Class::Method::Modifiers/after>.
104
105=head2 around (method|methods) => Code
106
107Sets up an "around" method modifier. See L<Moose/around> or
108L<Class::Method::Modifiers/around>.
109
110=head2 has (name|names) => parameters
111
112Sets up an attribute (or if passed an arrayref of names, multiple attributes) to
113this role. See L<Mouse/has>.
114
115=head2 confess error -> BOOM
116
117L<Carp/confess> for your convenience.
118
119=head2 blessed value -> ClassName | undef
120
121L<Scalar::Util/blessed> for your convenience.
122
123=head1 MISC
124
125=head2 import
126
127Importing Mouse::Role will give you sugar.
128
129=head2 unimport
130
131Please unimport Mouse (C<no Mouse::Role>) so that if someone calls one of the
132keywords (such as L</has>) it will break loudly instead breaking subtly.
133
134=cut
135