Make Moose and Moose::Role use strict
[gitmo/Moose.git] / lib / Moose / Role.pm
CommitLineData
e185c027 1package Moose::Role;
d1e17c7f 2use strict;
3use warnings;
e185c027 4
e65dccbc 5use Scalar::Util 'blessed';
f4f808de 6use Carp 'croak';
e185c027 7
2d562421 8use Sub::Exporter;
9
9e4ed568 10our $VERSION = '0.84';
e606ae5f 11$VERSION = eval $VERSION;
d44714be 12our $AUTHORITY = 'cpan:STEVAN';
e185c027 13
d7d8a8c7 14use Moose ();
15use Moose::Util ();
e65dccbc 16
c36b393c 17use Moose::Exporter;
e185c027 18use Moose::Meta::Role;
7eaef7ad 19use Moose::Util::TypeConstraints;
e185c027 20
e606ae5f 21sub extends {
887e3bf0 22 croak "Roles do not support 'extends' (you can use 'with' to specialize a role)";
e606ae5f 23}
24
25sub with {
26 Moose::Util::apply_all_roles( Moose::Meta::Role->initialize(shift), @_ );
27}
28
29sub requires {
30 my $meta = Moose::Meta::Role->initialize(shift);
31 croak "Must specify at least one method" unless @_;
32 $meta->add_required_methods(@_);
33}
34
35sub excludes {
36 my $meta = Moose::Meta::Role->initialize(shift);
37 croak "Must specify at least one role" unless @_;
38 $meta->add_excluded_roles(@_);
39}
2d562421 40
e606ae5f 41sub has {
42 my $meta = Moose::Meta::Role->initialize(shift);
43 my $name = shift;
44 croak 'Usage: has \'name\' => ( key => value, ... )' if @_ == 1;
833b56a7 45 my %options = ( definition_context => Moose::Util::_caller_info(), @_ );
e606ae5f 46 my $attrs = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ];
47 $meta->add_attribute( $_, %options ) for @$attrs;
48}
2d562421 49
2e3c7aa0 50sub _add_method_modifier {
51 my $type = shift;
e606ae5f 52 my $meta = Moose::Meta::Role->initialize(shift);
53 my $code = pop @_;
fb1e11d5 54
e606ae5f 55 for (@_) {
b8945921 56 croak "Roles do not currently support "
e606ae5f 57 . ref($_)
2e3c7aa0 58 . " references for $type method modifiers"
e606ae5f 59 if ref $_;
2e3c7aa0 60 my $add_method = "add_${type}_method_modifier";
61 $meta->$add_method( $_, $code );
e606ae5f 62 }
63}
2d562421 64
2e3c7aa0 65sub before { _add_method_modifier('before', @_) }
2d562421 66
2e3c7aa0 67sub after { _add_method_modifier('after', @_) }
fb1e11d5 68
2e3c7aa0 69sub around { _add_method_modifier('around', @_) }
fb1e11d5 70
e606ae5f 71# see Moose.pm for discussion
72sub super {
73 return unless $Moose::SUPER_BODY;
74 $Moose::SUPER_BODY->(@Moose::SUPER_ARGS);
75}
76
77sub override {
78 my $meta = Moose::Meta::Role->initialize(shift);
79 my ( $name, $code ) = @_;
80 $meta->add_override_method_modifier( $name, $code );
81}
82
83sub inner {
b8945921 84 croak "Roles cannot support 'inner'";
e606ae5f 85}
86
87sub augment {
b8945921 88 croak "Roles cannot support 'augment'";
e606ae5f 89}
90
c36b393c 91Moose::Exporter->setup_import_methods(
e606ae5f 92 with_caller => [
93 qw( with requires excludes has before after around override make_immutable )
94 ],
95 as_is => [
96 qw( extends super inner augment ),
97 \&Carp::confess,
98 \&Scalar::Util::blessed,
99 ],
100);
101
102sub init_meta {
103 shift;
104 my %args = @_;
105
70ea9161 106 my $role = $args{for_class};
107
108 unless ($role) {
109 require Moose;
110 Moose->throw_error("Cannot call init_meta without specifying a for_class");
111 }
e606ae5f 112
113 my $metaclass = $args{metaclass} || "Moose::Meta::Role";
114
115 # make a subtype for each Moose class
116 role_type $role unless find_type_constraint($role);
117
118 # FIXME copy from Moose.pm
119 my $meta;
120 if ($role->can('meta')) {
121 $meta = $role->meta();
70ea9161 122
123 unless ( blessed($meta) && $meta->isa('Moose::Meta::Role') ) {
124 require Moose;
125 Moose->throw_error("You already have a &meta function, but it does not return a Moose::Meta::Role");
126 }
e606ae5f 127 }
128 else {
129 $meta = $metaclass->initialize($role);
130
131 $meta->add_method(
132 'meta' => sub {
133 # re-initialize so it inherits properly
134 $metaclass->initialize( ref($_[0]) || $_[0] );
d31f9614 135 }
e606ae5f 136 );
d31f9614 137 }
e606ae5f 138
139 return $meta;
e185c027 140}
141
1421;
143
144__END__
145
146=pod
147
148=head1 NAME
149
150Moose::Role - The Moose Role
151
76d37e5a 152=head1 SYNOPSIS
153
154 package Eq;
85424612 155 use Moose::Role; # automatically turns on strict and warnings
fb1e11d5 156
e46edf94 157 requires 'equal';
fb1e11d5 158
159 sub no_equal {
76d37e5a 160 my ($self, $other) = @_;
161 !$self->equal($other);
162 }
fb1e11d5 163
76d37e5a 164 # ... then in your classes
fb1e11d5 165
76d37e5a 166 package Currency;
85424612 167 use Moose; # automatically turns on strict and warnings
fb1e11d5 168
76d37e5a 169 with 'Eq';
fb1e11d5 170
76d37e5a 171 sub equal {
172 my ($self, $other) = @_;
bdabd620 173 $self->as_float == $other->as_float;
76d37e5a 174 }
175
e185c027 176=head1 DESCRIPTION
177
69161799 178The concept of roles is documented in L<Moose::Manual::Role>. This document
179serves as API documentation.
76d37e5a 180
2c0cbef7 181=head1 EXPORTED FUNCTIONS
182
85424612 183Moose::Role currently supports all of the functions that L<Moose> exports, but
184differs slightly in how some items are handled (see L<CAVEATS> below for
185details).
76d37e5a 186
85424612 187Moose::Role also offers two role-specific keyword exports:
e185c027 188
189=over 4
190
2c0cbef7 191=item B<requires (@method_names)>
76d37e5a 192
fb1e11d5 193Roles can require that certain methods are implemented by any class which
85424612 194C<does> the role.
9e93dd19 195
2c0cbef7 196=item B<excludes (@role_names)>
197
9e93dd19 198Roles can C<exclude> other roles, in effect saying "I can never be combined
fb1e11d5 199with these C<@role_names>". This is a feature which should not be used
85424612 200lightly.
9e93dd19 201
2c0cbef7 202=back
203
d31f9614 204=head2 B<unimport>
205
206Moose::Role offers a way to remove the keywords it exports, through the
207C<unimport> method. You simply have to say C<no Moose::Role> at the bottom of
208your code for this to work.
209
e606ae5f 210=head2 B<< Moose::Role->init_meta(for_class => $role, metaclass => $metaclass) >>
211
212The C<init_meta> method sets up the metaclass object for the role
213specified by C<for_class>. It also injects a a C<meta> accessor into
214the role so you can get at this object.
215
216The default metaclass is L<Moose::Meta::Role>. You can specify an
217alternate metaclass with the C<metaclass> parameter.
218
c1381000 219=head1 METACLASS
220
221When you use Moose::Role, you can specify which metaclass to use:
222
223 use Moose::Role -metaclass => 'My::Meta::Role';
224
225You can also specify traits which will be applied to your role metaclass:
226
227 use Moose::Role -traits => 'My::Trait';
228
229This is very similar to the attribute traits feature. When you do
230this, your class's C<meta> object will have the specified traits
231applied to it. See L<Moose/TRAIT NAME RESOLUTION> for more details.
232
2c0cbef7 233=head1 CAVEATS
234
85424612 235Role support has only a few caveats:
2c0cbef7 236
237=over 4
76d37e5a 238
76d37e5a 239=item *
240
fb1e11d5 241Roles cannot use the C<extends> keyword; it will throw an exception for now.
242The same is true of the C<augment> and C<inner> keywords (not sure those
243really make sense for roles). All other Moose keywords will be I<deferred>
85424612 244so that they can be applied to the consuming class.
76d37e5a 245
fb1e11d5 246=item *
2c0cbef7 247
85424612 248Role composition does its best to B<not> be order-sensitive when it comes to
249conflict resolution and requirements detection. However, it is order-sensitive
250when it comes to method modifiers. All before/around/after modifiers are
251included whenever a role is composed into a class, and then applied in the order
252in which the roles are used. This also means that there is no conflict for
253before/around/after modifiers.
2c0cbef7 254
85424612 255In most cases, this will be a non-issue; however, it is something to keep in
256mind when using method modifiers in a role. You should never assume any
2c0cbef7 257ordering.
258
e185c027 259=back
260
261=head1 BUGS
262
fb1e11d5 263All complex software has bugs lurking in it, and this module is no
e185c027 264exception. If you find a bug please either email me, or add the bug
265to cpan-RT.
266
267=head1 AUTHOR
268
269Stevan Little E<lt>stevan@iinteractive.comE<gt>
270
db1ab48d 271Christian Hansen E<lt>chansen@cpan.orgE<gt>
98aae381 272
e185c027 273=head1 COPYRIGHT AND LICENSE
274
2840a3b2 275Copyright 2006-2009 by Infinity Interactive, Inc.
e185c027 276
277L<http://www.iinteractive.com>
278
279This library is free software; you can redistribute it and/or modify
fb1e11d5 280it under the same terms as Perl itself.
e185c027 281
68117c45 282=cut