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