11 use Scalar::Util 'blessed';
12 use Class::Method::Modifiers ();
14 use Mouse::Meta::Attribute;
15 use Mouse::Meta::Class;
17 use Mouse::TypeRegistry;
24 my $meta = Mouse::Meta::Class->initialize($CALLER);
31 $caller->meta->superclasses(@_);
39 $names = [$names] if !ref($names);
41 for my $name (@$names) {
42 if ($name =~ s/^\+//) {
43 Mouse::Meta::Attribute->clone_parent($package, $name, @_);
46 Mouse::Meta::Attribute->create($package, $name, @_);
61 return \&Class::Method::Modifiers::before;
65 return \&Class::Method::Modifiers::after;
69 return \&Class::Method::Modifiers::around;
77 my $class = $caller->meta;
79 Mouse::load_class($role);
80 $role->apply_to_class($class);
85 my $exporter = Sub::Exporter::build_exporter({
87 groups => { default => [':all'] },
96 my $meta = Mouse::Meta::Class->initialize($CALLER);
97 $meta->superclasses('Mouse::Object')
98 unless $meta->superclasses;
107 for my $keyword (keys %exports) {
108 next if $keyword eq 'meta'; # we don't delete this one
109 delete ${ $caller . '::' }{$keyword};
117 if (ref($class) || !defined($class) || !length($class)) {
118 my $display = defined($class) ? $class : 'undef';
119 confess "Invalid class name ($display)";
122 return 1 if is_class_loaded($class);
124 (my $file = "$class.pm") =~ s{::}{/}g;
126 eval { CORE::require($file) };
127 confess "Could not load class ($class) because : $@" if $@;
132 sub is_class_loaded {
135 return 0 if ref($class) || !defined($class) || !length($class);
137 # walk the symbol table tree to avoid autovififying
138 # \*{${main::}{"Foo::"}} == \*main::Foo::
141 foreach my $part (split('::', $class)) {
142 return 0 unless exists ${$$pack}{"${part}::"};
143 $pack = \*{${$$pack}{"${part}::"}};
146 # check for $VERSION or @ISA
147 return 1 if exists ${$$pack}{VERSION}
148 && defined *{${$$pack}{VERSION}}{SCALAR};
149 return 1 if exists ${$$pack}{ISA}
150 && defined *{${$$pack}{ISA}}{ARRAY};
152 # check for any method
153 foreach ( keys %{$$pack} ) {
154 next if substr($_, -2, 2) eq '::';
155 return 1 if defined *{${$$pack}{$_}}{CODE};
168 Mouse - Moose minus the antlers
173 use Mouse; # automatically turns on strict and warnings
175 has 'x' => (is => 'rw', isa => 'Int');
176 has 'y' => (is => 'rw', isa => 'Int');
189 has 'z' => (is => 'rw', isa => 'Int');
191 after 'clear' => sub {
198 L<Moose> is wonderful.
200 Unfortunately, it's a little slow. Though significant progress has been made
201 over the years, the compile time penalty is a non-starter for some
204 Mouse aims to alleviate this by providing a subset of Moose's
205 functionality, faster. In particular, L<Moose/has> is missing only a few
206 expert-level features.
210 Compatibility with Moose has been the utmost concern. Fewer than 1% of the
211 tests fail when run against Moose instead of Mouse. Mouse code coverage is also
212 over 99%. Even the error messages are taken from Moose. The Mouse code just
213 runs the test suite 3x-4x faster.
215 The idea is that, if you need the extra power, you should be able to run
216 C<s/Mouse/Moose/g> on your codebase and have nothing break. To that end,
217 nothingmuch has written L<Squirrel> (part of this distribution) which will act
218 as Mouse unless Moose is loaded, in which case it will act as Moose.
220 Mouse also has the blessings of Moose's author, stevan.
222 =head2 MISSING FEATURES
226 Fixing this one slightly less soon. stevan has suggested an implementation
227 strategy. Mouse currently mostly ignores methods.
231 User-defined type constraints and parameterized types may be implemented. Type
232 coercions probably not (patches welcome).
234 =head3 Bootstrapped meta world
236 Very handy for extensions to the MOP. Not pressing, but would be nice to have.
238 =head3 Modification of attribute metaclass
240 When you declare an attribute with L</has>, you get the inlined accessors
241 installed immediately. Modifying the attribute metaclass, even if possible,
250 =head2 meta -> Mouse::Meta::Class
252 Returns this class' metaclass instance.
254 =head2 extends superclasses
256 Sets this class' superclasses.
258 =head2 before (method|methods) => Code
260 Installs a "before" method modifier. See L<Moose/before> or
261 L<Class::Method::Modifiers/before>.
263 =head2 after (method|methods) => Code
265 Installs an "after" method modifier. See L<Moose/after> or
266 L<Class::Method::Modifiers/after>.
268 =head2 around (method|methods) => Code
270 Installs an "around" method modifier. See L<Moose/around> or
271 L<Class::Method::Modifiers/around>.
273 =head2 has (name|names) => parameters
275 Adds an attribute (or if passed an arrayref of names, multiple attributes) to
282 If specified, inlines a read-only/read-write accessor with the same name as
285 =item isa => TypeConstraint
287 Provides basic type checking in the constructor and accessor. Basic types such
288 as C<Int>, C<ArrayRef>, C<Defined> are supported. Any unknown type is taken to
289 be a class check (e.g. isa => 'DateTime' would accept only L<DateTime>
292 =item required => 0|1
294 Whether this attribute is required to have a value. If the attribute is lazy or
295 has a builder, then providing a value for the attribute in the constructor is
298 =item init_arg => Str
300 Allows you to use a different key name in the constructor.
302 =item default => Value | CodeRef
304 Sets the default value of the attribute. If the default is a coderef, it will
305 be invoked to get the default value. Due to quirks of Perl, any bare reference
306 is forbidden, you must wrap the reference in a coderef. Otherwise, all
307 instances will share the same reference.
311 If specified, the default is calculated on demand instead of in the
314 =item predicate => Str
316 Lets you specify a method name for installing a predicate method, which checks
317 that the attribute has a value. It will not invoke a lazy default or builder
322 Lets you specify a method name for installing a clearer method, which clears
323 the attribute's value from the instance. On the next read, lazy or builder will
326 =item handles => HashRef|ArrayRef
328 Lets you specify methods to delegate to the attribute. ArrayRef forwards the
329 given method names to method calls on the attribute. HashRef maps local method
330 names to remote method names called on the attribute. Other forms of
331 L</handles>, such as regular expression and coderef, are not yet supported.
333 =item weak_ref => 0|1
335 Lets you automatically weaken any reference stored in the attribute.
337 =item trigger => Coderef
339 Any time the attribute's value is set (either through the accessor or the
340 constructor), the trigger is called on it. The trigger receives as arguments
341 the instance, the new value, and the attribute instance.
345 Defines a method name to be called to provide the default value of the
346 attribute. C<< builder => 'build_foo' >> is mostly equivalent to
347 C<< default => sub { $_[0]->build_foo } >>.
349 =item auto_deref => 0|1
351 Allows you to automatically dereference ArrayRef and HashRef attributes in list
352 context. In scalar context, the reference is returned (NOT the list length or
353 bucket status). You must specify an appropriate type constraint to use
358 =head2 confess error -> BOOM
360 L<Carp/confess> for your convenience.
362 =head2 blessed value -> ClassName | undef
364 L<Scalar::Util/blessed> for your convenience.
370 Importing Mouse will default your class' superclass list to L<Mouse::Object>.
371 You may use L</extends> to replace the superclass list.
375 Please unimport Mouse (C<no Mouse>) so that if someone calls one of the
376 keywords (such as L</extends>) it will break loudly instead breaking subtly.
380 =head2 load_class Class::Name
382 This will load a given C<Class::Name> (or die if it's not loadable).
383 This function can be used in place of tricks like
384 C<eval "use $module"> or using C<require>.
386 =head2 is_class_loaded Class::Name -> Bool
388 Returns whether this class is actually loaded or not. It uses a heuristic which
389 involves checking for the existence of C<$VERSION>, C<@ISA>, and any
390 locally-defined method.
394 Shawn M Moore, C<< <sartak at gmail.com> >>
396 with plenty of code borrowed from L<Class::MOP> and L<Moose>
402 Please report any bugs through RT: email
403 C<bug-mouse at rt.cpan.org>, or browse
404 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mouse>.
406 =head1 COPYRIGHT AND LICENSE
408 Copyright 2008 Shawn M Moore.
410 This program is free software; you can redistribute it and/or modify it
411 under the same terms as Perl itself.