Fix Makefile.PL to use Mouse::Spec
[gitmo/Mouse.git] / lib / Mouse.pm
CommitLineData
c3398f5b 1package Mouse;
2use strict;
3use warnings;
eae80759 4use 5.006;
eaad7dab 5use base 'Exporter';
c3398f5b 6
2ba7f7cd 7our $VERSION = '0.32';
c3398f5b 8
c3398f5b 9use Carp 'confess';
6c169c50 10use Scalar::Util 'blessed';
6d28c5cf 11
0740bdfa 12use Mouse::Util qw(load_class is_class_loaded 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 ();
c3398f5b 20
2cb8b713 21our @EXPORT = qw(
22 extends with
23 has
24 before after around
25 override super
26 augment inner
27
28 blessed confess
29);
eaad7dab 30
3a63a2e7 31our %is_removable = map{ $_ => undef } @EXPORT;
32delete $is_removable{blessed};
33delete $is_removable{confess};
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);
60f6eba9 39 $meta->add_attribute(@_);
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
83 return if defined $SUPER_PACKAGE && $SUPER_PACKAGE ne caller();
84 return unless $SUPER_BODY; $SUPER_BODY->(@SUPER_ARGS);
85}
86
87sub override {
88 my $meta = Mouse::Meta::Class->initialize(caller);
89 my $pkg = $meta->name;
90
91 my $name = shift;
92 my $code = shift;
93
94 my $body = $pkg->can($name)
95 or confess "You cannot override '$name' because it has no super method";
96
97 $meta->add_method($name => sub {
98 local $SUPER_PACKAGE = $pkg;
99 local @SUPER_ARGS = @_;
100 local $SUPER_BODY = $body;
101
102 $code->(@_);
103 });
104}
105
2cb8b713 106sub inner { not_supported }
107sub augment{ not_supported }
108
0d6e12be 109sub init_meta {
0d6e12be 110 shift;
111 my %args = @_;
112
113 my $class = $args{for_class}
382b7340 114 or confess("Cannot call init_meta without specifying a for_class");
0d6e12be 115 my $base_class = $args{base_class} || 'Mouse::Object';
116 my $metaclass = $args{metaclass} || 'Mouse::Meta::Class';
117
382b7340 118 confess("The Metaclass $metaclass must be a subclass of Mouse::Meta::Class.")
0d6e12be 119 unless $metaclass->isa('Mouse::Meta::Class');
382b7340 120
0d6e12be 121 # make a subtype for each Mouse class
6d28c5cf 122 Mouse::Util::TypeConstraints::class_type($class)
123 unless Mouse::Util::TypeConstraints::find_type_constraint($class);
0d6e12be 124
125 my $meta = $metaclass->initialize($class);
0d6e12be 126
3a63a2e7 127 $meta->add_method(meta => sub{
382b7340 128 return $metaclass->initialize(ref($_[0]) || $_[0]);
3a63a2e7 129 });
130
382b7340 131 $meta->superclasses($base_class)
132 unless $meta->superclasses;
0d6e12be 133
134 return $meta;
135}
136
eaad7dab 137sub import {
e15d73d2 138 my $class = shift;
139
eaad7dab 140 strict->import;
141 warnings->import;
142
bfcc8a05 143 my $opts = do {
144 if (ref($_[0]) && ref($_[0]) eq 'HASH') {
145 shift @_;
146 } else {
147 +{ };
148 }
149 };
150 my $level = delete $opts->{into_level};
151 $level = 0 unless defined $level;
152 my $caller = caller($level);
eaad7dab 153
7daedfff 154 # we should never export to main
155 if ($caller eq 'main') {
156 warn qq{$class does not export its sugar to the 'main' package.\n};
157 return;
158 }
159
382b7340 160 $class->init_meta(
0d6e12be 161 for_class => $caller,
162 );
eaad7dab 163
e15d73d2 164 if (@_) {
bfcc8a05 165 __PACKAGE__->export_to_level( $level+1, $class, @_);
e15d73d2 166 } else {
167 # shortcut for the common case of no type character
168 no strict 'refs';
169 for my $keyword (@EXPORT) {
170 *{ $caller . '::' . $keyword } = *{__PACKAGE__ . '::' . $keyword};
171 }
172 }
eaad7dab 173}
174
175sub unimport {
176 my $caller = caller;
177
3a63a2e7 178 my $stash = do{
179 no strict 'refs';
180 \%{$caller . '::'}
181 };
182
eaad7dab 183 for my $keyword (@EXPORT) {
3a63a2e7 184 my $code;
185 if(exists $is_removable{$keyword}
186 && ($code = $caller->can($keyword))
187 && (Mouse::Util::get_code_info($code))[0] eq __PACKAGE__){
188
189 delete $stash->{$keyword};
190 }
eaad7dab 191 }
192}
c3398f5b 193
c3398f5b 1941;
195
196__END__
197
198=head1 NAME
199
0fff36e6 200Mouse - Moose minus the antlers
c3398f5b 201
c3398f5b 202=head1 SYNOPSIS
203
204 package Point;
6caea456 205 use Mouse; # automatically turns on strict and warnings
206
207 has 'x' => (is => 'rw', isa => 'Int');
208 has 'y' => (is => 'rw', isa => 'Int');
209
210 sub clear {
211 my $self = shift;
212 $self->x(0);
213 $self->y(0);
214 }
215
216 package Point3D;
c3398f5b 217 use Mouse;
218
6caea456 219 extends 'Point';
c3398f5b 220
6caea456 221 has 'z' => (is => 'rw', isa => 'Int');
222
8517d2ff 223 after 'clear' => sub {
224 my $self = shift;
225 $self->z(0);
226 };
c3398f5b 227
228=head1 DESCRIPTION
229
6614c108 230L<Moose> is wonderful. B<Use Moose instead of Mouse.>
c3398f5b 231
16913e71 232Unfortunately, Moose has a compile-time penalty. Though significant progress
233has been made over the years, the compile time penalty is a non-starter for
234some very specific applications. If you are writing a command-line application
235or CGI script where startup time is essential, you may not be able to use
236Moose. We recommend that you instead use L<HTTP::Engine> and FastCGI for the
237latter, if possible.
0fff36e6 238
6614c108 239Mouse aims to alleviate this by providing a subset of Moose's functionality,
240faster.
0fff36e6 241
7ca75105 242We're also going as light on dependencies as possible.
3fcf8a33 243L<Class::Method::Modifiers::Fast> or L<Class::Method::Modifiers> is required
244if you want support for L</before>, L</after>, and L</around>.
d1c1b994 245
0fff36e6 246=head2 MOOSE COMPAT
247
248Compatibility with Moose has been the utmost concern. Fewer than 1% of the
249tests fail when run against Moose instead of Mouse. Mouse code coverage is also
7e3ed32e 250over 96%. Even the error messages are taken from Moose. The Mouse code just
251runs the test suite 4x faster.
0fff36e6 252
253The idea is that, if you need the extra power, you should be able to run
8e1a28a8 254C<s/Mouse/Moose/g> on your codebase and have nothing break. To that end,
6614c108 255we have written L<Any::Moose> which will act as Mouse unless Moose is loaded,
16913e71 256in which case it will act as Moose. Since Mouse is a little sloppier than
257Moose, if you run into weird errors, it would be worth running:
258
259 ANY_MOOSE=Moose perl your-script.pl
260
261to see if the bug is caused by Mouse. Moose's diagnostics and validation are
262also much better.
0fff36e6 263
9090e5fe 264=head2 MouseX
265
266Please don't copy MooseX code to MouseX. If you need extensions, you really
267should upgrade to Moose. We don't need two parallel sets of extensions!
268
269If you really must write a Mouse extension, please contact the Moose mailing
270list or #moose on IRC beforehand.
271
56be135d 272=head2 Maintenance
273
274The original author of this module has mostly stepped down from maintaining
275Mouse. See L<http://www.nntp.perl.org/group/perl.moose/2009/04/msg653.html>.
276If you would like to help maintain this module, please get in touch with us.
277
0fff36e6 278=head1 KEYWORDS
c3398f5b 279
306290e8 280=head2 meta -> Mouse::Meta::Class
c3398f5b 281
282Returns this class' metaclass instance.
283
284=head2 extends superclasses
285
286Sets this class' superclasses.
287
b7a74822 288=head2 before (method|methods) => Code
289
290Installs a "before" method modifier. See L<Moose/before> or
291L<Class::Method::Modifiers/before>.
292
6beb7db6 293Use of this feature requires L<Class::Method::Modifiers>!
294
b7a74822 295=head2 after (method|methods) => Code
296
297Installs an "after" method modifier. See L<Moose/after> or
298L<Class::Method::Modifiers/after>.
299
6beb7db6 300Use of this feature requires L<Class::Method::Modifiers>!
301
b7a74822 302=head2 around (method|methods) => Code
303
304Installs an "around" method modifier. See L<Moose/around> or
305L<Class::Method::Modifiers/around>.
306
6beb7db6 307Use of this feature requires L<Class::Method::Modifiers>!
308
c3398f5b 309=head2 has (name|names) => parameters
310
311Adds an attribute (or if passed an arrayref of names, multiple attributes) to
0fff36e6 312this class. Options:
313
314=over 4
315
316=item is => ro|rw
317
318If specified, inlines a read-only/read-write accessor with the same name as
319the attribute.
320
321=item isa => TypeConstraint
322
5893ee36 323Provides type checking in the constructor and accessor. The following types are
324supported. Any unknown type is taken to be a class check (e.g. isa =>
325'DateTime' would accept only L<DateTime> objects).
326
327 Any Item Bool Undef Defined Value Num Int Str ClassName
328 Ref ScalarRef ArrayRef HashRef CodeRef RegexpRef GlobRef
329 FileHandle Object
330
331For more documentation on type constraints, see L<Mouse::Util::TypeConstraints>.
332
0fff36e6 333
334=item required => 0|1
335
336Whether this attribute is required to have a value. If the attribute is lazy or
337has a builder, then providing a value for the attribute in the constructor is
338optional.
339
ca63d17a 340=item init_arg => Str | Undef
0fff36e6 341
ca63d17a 342Allows you to use a different key name in the constructor. If undef, the
343attribue can't be passed to the constructor.
0fff36e6 344
345=item default => Value | CodeRef
346
347Sets the default value of the attribute. If the default is a coderef, it will
348be invoked to get the default value. Due to quirks of Perl, any bare reference
349is forbidden, you must wrap the reference in a coderef. Otherwise, all
350instances will share the same reference.
351
352=item lazy => 0|1
353
354If specified, the default is calculated on demand instead of in the
355constructor.
356
357=item predicate => Str
358
359Lets you specify a method name for installing a predicate method, which checks
360that the attribute has a value. It will not invoke a lazy default or builder
361method.
362
363=item clearer => Str
364
365Lets you specify a method name for installing a clearer method, which clears
366the attribute's value from the instance. On the next read, lazy or builder will
367be invoked.
368
369=item handles => HashRef|ArrayRef
370
371Lets you specify methods to delegate to the attribute. ArrayRef forwards the
372given method names to method calls on the attribute. HashRef maps local method
373names to remote method names called on the attribute. Other forms of
374L</handles>, such as regular expression and coderef, are not yet supported.
375
376=item weak_ref => 0|1
377
378Lets you automatically weaken any reference stored in the attribute.
379
6beb7db6 380Use of this feature requires L<Scalar::Util>!
381
844fa049 382=item trigger => CodeRef
383
384Any 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.
385
6beb7db6 386Mouse 0.05 supported more complex triggers, but this behavior is now removed.
0fff36e6 387
388=item builder => Str
389
390Defines a method name to be called to provide the default value of the
391attribute. C<< builder => 'build_foo' >> is mostly equivalent to
392C<< default => sub { $_[0]->build_foo } >>.
393
394=item auto_deref => 0|1
395
396Allows you to automatically dereference ArrayRef and HashRef attributes in list
397context. In scalar context, the reference is returned (NOT the list length or
398bucket status). You must specify an appropriate type constraint to use
399auto_deref.
400
5253d13d 401=item lazy_build => 0|1
402
403Automatically define lazy => 1 as well as builder => "_build_$attr", clearer =>
404"clear_$attr', predicate => 'has_$attr' unless they are already defined.
405
0fff36e6 406=back
c3398f5b 407
408=head2 confess error -> BOOM
409
410L<Carp/confess> for your convenience.
411
412=head2 blessed value -> ClassName | undef
413
414L<Scalar::Util/blessed> for your convenience.
415
416=head1 MISC
417
418=head2 import
419
6caea456 420Importing Mouse will default your class' superclass list to L<Mouse::Object>.
c3398f5b 421You may use L</extends> to replace the superclass list.
422
423=head2 unimport
424
0fff36e6 425Please unimport Mouse (C<no Mouse>) so that if someone calls one of the
426keywords (such as L</extends>) it will break loudly instead breaking subtly.
c3398f5b 427
428=head1 FUNCTIONS
429
430=head2 load_class Class::Name
431
6caea456 432This will load a given C<Class::Name> (or die if it's not loadable).
c3398f5b 433This function can be used in place of tricks like
434C<eval "use $module"> or using C<require>.
435
262801ef 436=head2 is_class_loaded Class::Name -> Bool
437
438Returns whether this class is actually loaded or not. It uses a heuristic which
439involves checking for the existence of C<$VERSION>, C<@ISA>, and any
440locally-defined method.
441
e693975f 442=head1 SOURCE CODE ACCESS
443
444We have a public git repo:
445
446 git clone git://jules.scsys.co.uk/gitmo/Mouse.git
447
c817e8f1 448=head1 AUTHORS
c3398f5b 449
450Shawn M Moore, C<< <sartak at gmail.com> >>
451
fc9f8988 452Yuval Kogman, C<< <nothingmuch at woobling.org> >>
453
c817e8f1 454tokuhirom
455
456Yappo
457
ea6dc3a5 458wu-lee
459
ba55dea1 460Goro Fuji (gfx) C<< <gfuji at cpan.org> >>
461
0fff36e6 462with plenty of code borrowed from L<Class::MOP> and L<Moose>
463
c3398f5b 464=head1 BUGS
465
9e3c345e 466There is a known issue with Mouse on 5.6.2 regarding the @ISA tests. Until
467this is resolve the minimum version of Perl for Mouse is set to 5.8.0. Patches
468to resolve these tests are more than welcome.
c3398f5b 469
470Please report any bugs through RT: email
471C<bug-mouse at rt.cpan.org>, or browse
472L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mouse>.
473
474=head1 COPYRIGHT AND LICENSE
475
3ef6ae56 476Copyright 2008-2009 Infinity Interactive, Inc.
477
478http://www.iinteractive.com/
c3398f5b 479
480This program is free software; you can redistribute it and/or modify it
481under the same terms as Perl itself.
482
483=cut
484