no_plan is so lazy
[gitmo/Mouse.git] / lib / Mouse.pm
CommitLineData
c3398f5b 1#!perl
2package Mouse;
3use strict;
4use warnings;
5
6af45c5e 6our $VERSION = '0.05';
126765f0 7use 5.006;
c3398f5b 8
9use Sub::Exporter;
10use Carp 'confess';
11use Scalar::Util 'blessed';
8517d2ff 12use Class::Method::Modifiers ();
c3398f5b 13
306290e8 14use Mouse::Meta::Attribute;
15use Mouse::Meta::Class;
c3398f5b 16use Mouse::Object;
d60c78b9 17use Mouse::TypeRegistry;
c3398f5b 18
19do {
20 my $CALLER;
21
22 my %exports = (
23 meta => sub {
306290e8 24 my $meta = Mouse::Meta::Class->initialize($CALLER);
c3398f5b 25 return sub { $meta };
26 },
27
28 extends => sub {
29 my $caller = $CALLER;
30 return sub {
31 $caller->meta->superclasses(@_);
32 };
33 },
34
35 has => sub {
36 return sub {
37 my $package = caller;
38 my $names = shift;
39 $names = [$names] if !ref($names);
40
41 for my $name (@$names) {
1bfebf5f 42 if ($name =~ s/^\+//) {
43 Mouse::Meta::Attribute->clone_parent($package, $name, @_);
44 }
45 else {
46 Mouse::Meta::Attribute->create($package, $name, @_);
47 }
c3398f5b 48 }
49 };
50 },
51
52 confess => sub {
b17094ce 53 return \&confess;
c3398f5b 54 },
55
56 blessed => sub {
b17094ce 57 return \&blessed;
c3398f5b 58 },
8517d2ff 59
60 before => sub {
61 return \&Class::Method::Modifiers::before;
62 },
63
64 after => sub {
65 return \&Class::Method::Modifiers::after;
66 },
67
68 around => sub {
69 return \&Class::Method::Modifiers::around;
70 },
3ab0ee8f 71
72 with => sub {
73 my $caller = $CALLER;
74
75 return sub {
76 my $role = shift;
77 my $class = $caller->meta;
78
e5f31f1f 79 confess "Mouse::Role only supports 'with' on individual roles at a time" if @_;
e7dff5dc 80
3ab0ee8f 81 Mouse::load_class($role);
da0c885d 82 $role->meta->apply($class);
3ab0ee8f 83 };
84 },
c3398f5b 85 );
86
87 my $exporter = Sub::Exporter::build_exporter({
88 exports => \%exports,
89 groups => { default => [':all'] },
90 });
91
92 sub import {
93 $CALLER = caller;
94
95 strict->import;
96 warnings->import;
97
306290e8 98 my $meta = Mouse::Meta::Class->initialize($CALLER);
ca73a208 99 $meta->superclasses('Mouse::Object')
100 unless $meta->superclasses;
c3398f5b 101
102 goto $exporter;
103 }
104
105 sub unimport {
106 my $caller = caller;
107
108 no strict 'refs';
109 for my $keyword (keys %exports) {
110 next if $keyword eq 'meta'; # we don't delete this one
111 delete ${ $caller . '::' }{$keyword};
112 }
113 }
114};
115
116sub load_class {
117 my $class = shift;
262801ef 118
9694b71b 119 if (ref($class) || !defined($class) || !length($class)) {
120 my $display = defined($class) ? $class : 'undef';
121 confess "Invalid class name ($display)";
122 }
c3398f5b 123
2a674d23 124 return 1 if is_class_loaded($class);
125
c3398f5b 126 (my $file = "$class.pm") =~ s{::}{/}g;
127
128 eval { CORE::require($file) };
2a674d23 129 confess "Could not load class ($class) because : $@" if $@;
c3398f5b 130
131 return 1;
132}
133
2a674d23 134sub is_class_loaded {
135 my $class = shift;
136
7ecc2123 137 return 0 if ref($class) || !defined($class) || !length($class);
138
bf134049 139 # walk the symbol table tree to avoid autovififying
140 # \*{${main::}{"Foo::"}} == \*main::Foo::
141
142 my $pack = \*::;
143 foreach my $part (split('::', $class)) {
144 return 0 unless exists ${$$pack}{"${part}::"};
145 $pack = \*{${$$pack}{"${part}::"}};
2a674d23 146 }
bf134049 147
148 # check for $VERSION or @ISA
149 return 1 if exists ${$$pack}{VERSION}
150 && defined *{${$$pack}{VERSION}}{SCALAR};
151 return 1 if exists ${$$pack}{ISA}
152 && defined *{${$$pack}{ISA}}{ARRAY};
153
154 # check for any method
155 foreach ( keys %{$$pack} ) {
156 next if substr($_, -2, 2) eq '::';
157 return 1 if defined *{${$$pack}{$_}}{CODE};
158 }
159
160 # fail
2a674d23 161 return 0;
162}
163
c3398f5b 1641;
165
166__END__
167
168=head1 NAME
169
0fff36e6 170Mouse - Moose minus the antlers
c3398f5b 171
c3398f5b 172=head1 SYNOPSIS
173
174 package Point;
6caea456 175 use Mouse; # automatically turns on strict and warnings
176
177 has 'x' => (is => 'rw', isa => 'Int');
178 has 'y' => (is => 'rw', isa => 'Int');
179
180 sub clear {
181 my $self = shift;
182 $self->x(0);
183 $self->y(0);
184 }
185
186 package Point3D;
c3398f5b 187 use Mouse;
188
6caea456 189 extends 'Point';
c3398f5b 190
6caea456 191 has 'z' => (is => 'rw', isa => 'Int');
192
8517d2ff 193 after 'clear' => sub {
194 my $self = shift;
195 $self->z(0);
196 };
c3398f5b 197
198=head1 DESCRIPTION
199
0fff36e6 200L<Moose> is wonderful.
c3398f5b 201
0fff36e6 202Unfortunately, it's a little slow. Though significant progress has been made
203over the years, the compile time penalty is a non-starter for some
204applications.
205
206Mouse aims to alleviate this by providing a subset of Moose's
207functionality, faster. In particular, L<Moose/has> is missing only a few
208expert-level features.
209
210=head2 MOOSE COMPAT
211
212Compatibility with Moose has been the utmost concern. Fewer than 1% of the
213tests fail when run against Moose instead of Mouse. Mouse code coverage is also
8e1a28a8 214over 99%. Even the error messages are taken from Moose. The Mouse code just
215runs the test suite 3x-4x faster.
0fff36e6 216
217The idea is that, if you need the extra power, you should be able to run
8e1a28a8 218C<s/Mouse/Moose/g> on your codebase and have nothing break. To that end,
219nothingmuch has written L<Squirrel> (part of this distribution) which will act
220as Mouse unless Moose is loaded, in which case it will act as Moose.
0fff36e6 221
222Mouse also has the blessings of Moose's author, stevan.
223
224=head2 MISSING FEATURES
225
0fff36e6 226=head3 Roles
227
228Fixing this one slightly less soon. stevan has suggested an implementation
229strategy. Mouse currently mostly ignores methods.
230
231=head3 Complex types
232
233User-defined type constraints and parameterized types may be implemented. Type
234coercions probably not (patches welcome).
235
236=head3 Bootstrapped meta world
237
238Very handy for extensions to the MOP. Not pressing, but would be nice to have.
239
240=head3 Modification of attribute metaclass
241
242When you declare an attribute with L</has>, you get the inlined accessors
243installed immediately. Modifying the attribute metaclass, even if possible,
244does nothing.
245
246=head3 Lots more..
247
248MouseX?
249
250=head1 KEYWORDS
c3398f5b 251
306290e8 252=head2 meta -> Mouse::Meta::Class
c3398f5b 253
254Returns this class' metaclass instance.
255
256=head2 extends superclasses
257
258Sets this class' superclasses.
259
b7a74822 260=head2 before (method|methods) => Code
261
262Installs a "before" method modifier. See L<Moose/before> or
263L<Class::Method::Modifiers/before>.
264
265=head2 after (method|methods) => Code
266
267Installs an "after" method modifier. See L<Moose/after> or
268L<Class::Method::Modifiers/after>.
269
270=head2 around (method|methods) => Code
271
272Installs an "around" method modifier. See L<Moose/around> or
273L<Class::Method::Modifiers/around>.
274
c3398f5b 275=head2 has (name|names) => parameters
276
277Adds an attribute (or if passed an arrayref of names, multiple attributes) to
0fff36e6 278this class. Options:
279
280=over 4
281
282=item is => ro|rw
283
284If specified, inlines a read-only/read-write accessor with the same name as
285the attribute.
286
287=item isa => TypeConstraint
288
289Provides basic type checking in the constructor and accessor. Basic types such
290as C<Int>, C<ArrayRef>, C<Defined> are supported. Any unknown type is taken to
291be a class check (e.g. isa => 'DateTime' would accept only L<DateTime>
292objects).
293
294=item required => 0|1
295
296Whether this attribute is required to have a value. If the attribute is lazy or
297has a builder, then providing a value for the attribute in the constructor is
298optional.
299
300=item init_arg => Str
301
302Allows you to use a different key name in the constructor.
303
304=item default => Value | CodeRef
305
306Sets the default value of the attribute. If the default is a coderef, it will
307be invoked to get the default value. Due to quirks of Perl, any bare reference
308is forbidden, you must wrap the reference in a coderef. Otherwise, all
309instances will share the same reference.
310
311=item lazy => 0|1
312
313If specified, the default is calculated on demand instead of in the
314constructor.
315
316=item predicate => Str
317
318Lets you specify a method name for installing a predicate method, which checks
319that the attribute has a value. It will not invoke a lazy default or builder
320method.
321
322=item clearer => Str
323
324Lets you specify a method name for installing a clearer method, which clears
325the attribute's value from the instance. On the next read, lazy or builder will
326be invoked.
327
328=item handles => HashRef|ArrayRef
329
330Lets you specify methods to delegate to the attribute. ArrayRef forwards the
331given method names to method calls on the attribute. HashRef maps local method
332names to remote method names called on the attribute. Other forms of
333L</handles>, such as regular expression and coderef, are not yet supported.
334
335=item weak_ref => 0|1
336
337Lets you automatically weaken any reference stored in the attribute.
338
339=item trigger => Coderef
340
341Any time the attribute's value is set (either through the accessor or the
342constructor), the trigger is called on it. The trigger receives as arguments
343the instance, the new value, and the attribute instance.
344
345=item builder => Str
346
347Defines a method name to be called to provide the default value of the
348attribute. C<< builder => 'build_foo' >> is mostly equivalent to
349C<< default => sub { $_[0]->build_foo } >>.
350
351=item auto_deref => 0|1
352
353Allows you to automatically dereference ArrayRef and HashRef attributes in list
354context. In scalar context, the reference is returned (NOT the list length or
355bucket status). You must specify an appropriate type constraint to use
356auto_deref.
357
358=back
c3398f5b 359
360=head2 confess error -> BOOM
361
362L<Carp/confess> for your convenience.
363
364=head2 blessed value -> ClassName | undef
365
366L<Scalar::Util/blessed> for your convenience.
367
368=head1 MISC
369
370=head2 import
371
6caea456 372Importing Mouse will default your class' superclass list to L<Mouse::Object>.
c3398f5b 373You may use L</extends> to replace the superclass list.
374
375=head2 unimport
376
0fff36e6 377Please unimport Mouse (C<no Mouse>) so that if someone calls one of the
378keywords (such as L</extends>) it will break loudly instead breaking subtly.
c3398f5b 379
380=head1 FUNCTIONS
381
382=head2 load_class Class::Name
383
6caea456 384This will load a given C<Class::Name> (or die if it's not loadable).
c3398f5b 385This function can be used in place of tricks like
386C<eval "use $module"> or using C<require>.
387
262801ef 388=head2 is_class_loaded Class::Name -> Bool
389
390Returns whether this class is actually loaded or not. It uses a heuristic which
391involves checking for the existence of C<$VERSION>, C<@ISA>, and any
392locally-defined method.
393
c3398f5b 394=head1 AUTHOR
395
396Shawn M Moore, C<< <sartak at gmail.com> >>
397
0fff36e6 398with plenty of code borrowed from L<Class::MOP> and L<Moose>
399
c3398f5b 400=head1 BUGS
401
402No known bugs.
403
404Please report any bugs through RT: email
405C<bug-mouse at rt.cpan.org>, or browse
406L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mouse>.
407
408=head1 COPYRIGHT AND LICENSE
409
410Copyright 2008 Shawn M Moore.
411
412This program is free software; you can redistribute it and/or modify it
413under the same terms as Perl itself.
414
415=cut
416