Check superclasses not to inherit from roles
[gitmo/Mouse.git] / lib / Mouse.pm
CommitLineData
c3398f5b 1package Mouse;
eb88905f 2use 5.006_002;
3
bc69ee88 4use Mouse::Exporter; # enables strict and warnings
eb88905f 5
8f14f3a4 6our $VERSION = '0.37_03';
c3398f5b 7
fea36fd2 8use Carp qw(confess);
43523060 9use Scalar::Util qw(blessed);
6d28c5cf 10
01afd8ff 11use Mouse::Util qw(load_class is_class_loaded get_code_package not_supported);
c3398f5b 12
08f7a8db 13use Mouse::Meta::Module;
306290e8 14use Mouse::Meta::Class;
7a50b450 15use Mouse::Meta::Role;
6d28c5cf 16use Mouse::Meta::Attribute;
c3398f5b 17use Mouse::Object;
6d28c5cf 18use Mouse::Util::TypeConstraints ();
fea36fd2 19
20Mouse::Exporter->setup_import_methods(
21 as_is => [qw(
22 extends with
23 has
24 before after around
25 override super
26 augment inner
27 ),
28 \&Scalar::Util::blessed,
29 \&Carp::confess,
30 ],
2cb8b713 31);
eaad7dab 32
43523060 33# XXX: for backward compatibility
34our @EXPORT = qw(
35 extends with
36 has
37 before after around
38 override super
39 augment inner
40 blessed confess
41);
42
8bc2760b 43sub extends { Mouse::Meta::Class->initialize(scalar caller)->superclasses(@_) }
c3398f5b 44
eaad7dab 45sub has {
8bc2760b 46 my $meta = Mouse::Meta::Class->initialize(scalar caller);
1b9e472d 47 my $name = shift;
48
49 $meta->add_attribute($_ => @_) for ref($name) ? @{$name} : $name;
eaad7dab 50}
51
52sub before {
8bc2760b 53 my $meta = Mouse::Meta::Class->initialize(scalar caller);
eaad7dab 54
55 my $code = pop;
56
57 for (@_) {
58 $meta->add_before_method_modifier($_ => $code);
59 }
60}
61
62sub after {
8bc2760b 63 my $meta = Mouse::Meta::Class->initialize(scalar caller);
eaad7dab 64
65 my $code = pop;
66
67 for (@_) {
68 $meta->add_after_method_modifier($_ => $code);
69 }
70}
71
72sub around {
8bc2760b 73 my $meta = Mouse::Meta::Class->initialize(scalar caller);
eaad7dab 74
75 my $code = pop;
76
77 for (@_) {
78 $meta->add_around_method_modifier($_ => $code);
79 }
80}
81
82sub with {
8bc2760b 83 Mouse::Util::apply_all_roles(scalar(caller), @_);
eaad7dab 84}
85
e6007308 86our $SUPER_PACKAGE;
87our $SUPER_BODY;
88our @SUPER_ARGS;
89
90sub super {
91 # This check avoids a recursion loop - see
92 # t/100_bugs/020_super_recursion.t
85bd3f44 93 return if defined $SUPER_PACKAGE && $SUPER_PACKAGE ne caller();
94 return if !defined $SUPER_BODY;
95 $SUPER_BODY->(@SUPER_ARGS);
e6007308 96}
97
98sub override {
85bd3f44 99 # my($name, $method) = @_;
100 Mouse::Meta::Class->initialize(scalar caller)->add_override_method_modifier(@_);
e6007308 101}
102
768804c0 103our %INNER_BODY;
104our %INNER_ARGS;
105
106sub inner {
107 my $pkg = caller();
108 if ( my $body = $INNER_BODY{$pkg} ) {
109 my $args = $INNER_ARGS{$pkg};
110 local $INNER_ARGS{$pkg};
111 local $INNER_BODY{$pkg};
112 return $body->(@{$args});
113 }
114 else {
115 return;
116 }
117}
118
119sub augment {
120 #my($name, $method) = @_;
121 Mouse::Meta::Class->initialize(scalar caller)->add_augment_method_modifier(@_);
122}
2cb8b713 123
0d6e12be 124sub init_meta {
0d6e12be 125 shift;
126 my %args = @_;
127
128 my $class = $args{for_class}
382b7340 129 or confess("Cannot call init_meta without specifying a for_class");
0d6e12be 130 my $base_class = $args{base_class} || 'Mouse::Object';
131 my $metaclass = $args{metaclass} || 'Mouse::Meta::Class';
132
0d6e12be 133 # make a subtype for each Mouse class
6d28c5cf 134 Mouse::Util::TypeConstraints::class_type($class)
135 unless Mouse::Util::TypeConstraints::find_type_constraint($class);
0d6e12be 136
137 my $meta = $metaclass->initialize($class);
0d6e12be 138
3a63a2e7 139 $meta->add_method(meta => sub{
382b7340 140 return $metaclass->initialize(ref($_[0]) || $_[0]);
3a63a2e7 141 });
142
382b7340 143 $meta->superclasses($base_class)
144 unless $meta->superclasses;
0d6e12be 145
146 return $meta;
147}
148
c3398f5b 149
c3398f5b 1501;
151
152__END__
153
154=head1 NAME
155
0fff36e6 156Mouse - Moose minus the antlers
c3398f5b 157
c3398f5b 158=head1 SYNOPSIS
159
160 package Point;
6caea456 161 use Mouse; # automatically turns on strict and warnings
162
163 has 'x' => (is => 'rw', isa => 'Int');
164 has 'y' => (is => 'rw', isa => 'Int');
165
166 sub clear {
167 my $self = shift;
168 $self->x(0);
169 $self->y(0);
170 }
171
172 package Point3D;
c3398f5b 173 use Mouse;
174
6caea456 175 extends 'Point';
c3398f5b 176
6caea456 177 has 'z' => (is => 'rw', isa => 'Int');
178
8517d2ff 179 after 'clear' => sub {
180 my $self = shift;
181 $self->z(0);
182 };
c3398f5b 183
184=head1 DESCRIPTION
185
6614c108 186L<Moose> is wonderful. B<Use Moose instead of Mouse.>
c3398f5b 187
16913e71 188Unfortunately, Moose has a compile-time penalty. Though significant progress
189has been made over the years, the compile time penalty is a non-starter for
190some very specific applications. If you are writing a command-line application
191or CGI script where startup time is essential, you may not be able to use
192Moose. We recommend that you instead use L<HTTP::Engine> and FastCGI for the
193latter, if possible.
0fff36e6 194
6614c108 195Mouse aims to alleviate this by providing a subset of Moose's functionality,
196faster.
0fff36e6 197
7ca75105 198We're also going as light on dependencies as possible.
3fcf8a33 199L<Class::Method::Modifiers::Fast> or L<Class::Method::Modifiers> is required
200if you want support for L</before>, L</after>, and L</around>.
d1c1b994 201
1820fffe 202=head2 MOOSE COMPATIBILITY
0fff36e6 203
204Compatibility with Moose has been the utmost concern. Fewer than 1% of the
205tests fail when run against Moose instead of Mouse. Mouse code coverage is also
7e3ed32e 206over 96%. Even the error messages are taken from Moose. The Mouse code just
207runs the test suite 4x faster.
0fff36e6 208
209The idea is that, if you need the extra power, you should be able to run
8e1a28a8 210C<s/Mouse/Moose/g> on your codebase and have nothing break. To that end,
6614c108 211we have written L<Any::Moose> which will act as Mouse unless Moose is loaded,
16913e71 212in which case it will act as Moose. Since Mouse is a little sloppier than
213Moose, if you run into weird errors, it would be worth running:
214
215 ANY_MOOSE=Moose perl your-script.pl
216
217to see if the bug is caused by Mouse. Moose's diagnostics and validation are
218also much better.
0fff36e6 219
9090e5fe 220=head2 MouseX
221
222Please don't copy MooseX code to MouseX. If you need extensions, you really
223should upgrade to Moose. We don't need two parallel sets of extensions!
224
225If you really must write a Mouse extension, please contact the Moose mailing
226list or #moose on IRC beforehand.
227
0fff36e6 228=head1 KEYWORDS
c3398f5b 229
1820fffe 230=head2 C<< $object->meta -> Mouse::Meta::Class >>
c3398f5b 231
232Returns this class' metaclass instance.
233
1820fffe 234=head2 C<< extends superclasses >>
c3398f5b 235
236Sets this class' superclasses.
237
1820fffe 238=head2 C<< before (method|methods) => CodeRef >>
b7a74822 239
240Installs a "before" method modifier. See L<Moose/before> or
241L<Class::Method::Modifiers/before>.
242
6beb7db6 243Use of this feature requires L<Class::Method::Modifiers>!
244
1820fffe 245=head2 C<< after (method|methods) => CodeRef >>
b7a74822 246
247Installs an "after" method modifier. See L<Moose/after> or
248L<Class::Method::Modifiers/after>.
249
6beb7db6 250Use of this feature requires L<Class::Method::Modifiers>!
251
1820fffe 252=head2 C<< around (method|methods) => CodeRef >>
b7a74822 253
254Installs an "around" method modifier. See L<Moose/around> or
255L<Class::Method::Modifiers/around>.
256
6beb7db6 257Use of this feature requires L<Class::Method::Modifiers>!
258
1820fffe 259=head2 C<< has (name|names) => parameters >>
c3398f5b 260
261Adds an attribute (or if passed an arrayref of names, multiple attributes) to
0fff36e6 262this class. Options:
263
264=over 4
265
1820fffe 266=item C<< is => ro|rw|bare >>
0fff36e6 267
268If specified, inlines a read-only/read-write accessor with the same name as
269the attribute.
270
1820fffe 271=item C<< isa => TypeConstraint >>
0fff36e6 272
5893ee36 273Provides type checking in the constructor and accessor. The following types are
1820fffe 274supported. Any unknown type is taken to be a class check
275(e.g. C<< isa => 'DateTime' >> would accept only L<DateTime> objects).
5893ee36 276
277 Any Item Bool Undef Defined Value Num Int Str ClassName
278 Ref ScalarRef ArrayRef HashRef CodeRef RegexpRef GlobRef
279 FileHandle Object
280
281For more documentation on type constraints, see L<Mouse::Util::TypeConstraints>.
282
0fff36e6 283
1820fffe 284=item C<< required => Bool >>
0fff36e6 285
286Whether this attribute is required to have a value. If the attribute is lazy or
287has a builder, then providing a value for the attribute in the constructor is
288optional.
289
1820fffe 290=item C<< init_arg => Str | Undef >>
0fff36e6 291
ca63d17a 292Allows you to use a different key name in the constructor. If undef, the
1820fffe 293attribute can't be passed to the constructor.
0fff36e6 294
1820fffe 295=item C<< default => Value | CodeRef >>
0fff36e6 296
297Sets the default value of the attribute. If the default is a coderef, it will
298be invoked to get the default value. Due to quirks of Perl, any bare reference
299is forbidden, you must wrap the reference in a coderef. Otherwise, all
300instances will share the same reference.
301
1820fffe 302=item C<< lazy => Bool >>
0fff36e6 303
304If specified, the default is calculated on demand instead of in the
305constructor.
306
1820fffe 307=item C<< predicate => Str >>
0fff36e6 308
309Lets you specify a method name for installing a predicate method, which checks
310that the attribute has a value. It will not invoke a lazy default or builder
311method.
312
1820fffe 313=item C<< clearer => Str >>
0fff36e6 314
315Lets you specify a method name for installing a clearer method, which clears
316the attribute's value from the instance. On the next read, lazy or builder will
317be invoked.
318
1820fffe 319=item C<< handles => HashRef|ArrayRef >>
0fff36e6 320
321Lets you specify methods to delegate to the attribute. ArrayRef forwards the
322given method names to method calls on the attribute. HashRef maps local method
323names to remote method names called on the attribute. Other forms of
324L</handles>, such as regular expression and coderef, are not yet supported.
325
1820fffe 326=item C<< weak_ref => Bool >>
0fff36e6 327
328Lets you automatically weaken any reference stored in the attribute.
329
6beb7db6 330Use of this feature requires L<Scalar::Util>!
331
1820fffe 332=item C<< trigger => CodeRef >>
844fa049 333
334Any time the attribute's value is set (either through the accessor or the constructor), the trigger is called on it. The trigger receives as arguments the instance, the new value, and the attribute instance.
335
1820fffe 336=item C<< builder => Str >>
0fff36e6 337
338Defines a method name to be called to provide the default value of the
339attribute. C<< builder => 'build_foo' >> is mostly equivalent to
340C<< default => sub { $_[0]->build_foo } >>.
341
1820fffe 342=item C<< auto_deref => Bool >>
0fff36e6 343
344Allows you to automatically dereference ArrayRef and HashRef attributes in list
345context. In scalar context, the reference is returned (NOT the list length or
346bucket status). You must specify an appropriate type constraint to use
347auto_deref.
348
1820fffe 349=item C<< lazy_build => Bool >>
350
351Automatically define the following options:
5253d13d 352
1820fffe 353 has $attr => (
354 # ...
355 lazy => 1
356 builder => "_build_$attr",
357 clearer => "clear_$attr",
358 predicate => "has_$attr",
359 );
5253d13d 360
0fff36e6 361=back
c3398f5b 362
1820fffe 363=head2 C<< confess(message) -> BOOM >>
c3398f5b 364
365L<Carp/confess> for your convenience.
366
1820fffe 367=head2 C<< blessed(value) -> ClassName | undef >>
c3398f5b 368
369L<Scalar::Util/blessed> for your convenience.
370
371=head1 MISC
372
373=head2 import
374
6caea456 375Importing Mouse will default your class' superclass list to L<Mouse::Object>.
c3398f5b 376You may use L</extends> to replace the superclass list.
377
378=head2 unimport
379
0fff36e6 380Please unimport Mouse (C<no Mouse>) so that if someone calls one of the
381keywords (such as L</extends>) it will break loudly instead breaking subtly.
c3398f5b 382
1820fffe 383=head1 SOURCE CODE ACCESS
c3398f5b 384
1820fffe 385We have a public git repository:
c3398f5b 386
1820fffe 387 git clone git://jules.scsys.co.uk/gitmo/Mouse.git
c3398f5b 388
1820fffe 389=head1 DEPENDENCIES
262801ef 390
1820fffe 391Perl 5.6.2 or later.
262801ef 392
1820fffe 393=head1 SEE ALSO
e693975f 394
1820fffe 395L<Moose>
e693975f 396
1820fffe 397L<Class::MOP>
e693975f 398
c817e8f1 399=head1 AUTHORS
c3398f5b 400
434ca269 401Shawn M Moore, E<lt>sartak at gmail.comE<gt>
c3398f5b 402
434ca269 403Yuval Kogman, E<lt>nothingmuch at woobling.orgE<gt>
fc9f8988 404
c817e8f1 405tokuhirom
406
407Yappo
408
ea6dc3a5 409wu-lee
410
434ca269 411Goro Fuji (gfx) E<lt>gfuji at cpan.orgE<gt>
ba55dea1 412
0fff36e6 413with plenty of code borrowed from L<Class::MOP> and L<Moose>
414
c3398f5b 415=head1 BUGS
416
1820fffe 417All complex software has bugs lurking in it, and this module is no exception.
418Please report any bugs to C<bug-mouse at rt.cpan.org>, or through the web
419interface at L<http://rt.cpan.org/Public/Dist/Display.html?Name=Mouse>
c3398f5b 420
421=head1 COPYRIGHT AND LICENSE
422
3ef6ae56 423Copyright 2008-2009 Infinity Interactive, Inc.
424
425http://www.iinteractive.com/
c3398f5b 426
427This program is free software; you can redistribute it and/or modify it
428under the same terms as Perl itself.
429
430=cut
431