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