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