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