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 Mouse::Meta::Attribute->create($package, $name, @_);
56 return \&Class::Method::Modifiers::before;
60 return \&Class::Method::Modifiers::after;
64 return \&Class::Method::Modifiers::around;
68 my $exporter = Sub::Exporter::build_exporter({
70 groups => { default => [':all'] },
79 my $meta = Mouse::Meta::Class->initialize($CALLER);
80 $meta->superclasses('Mouse::Object')
81 unless $meta->superclasses;
90 for my $keyword (keys %exports) {
91 next if $keyword eq 'meta'; # we don't delete this one
92 delete ${ $caller . '::' }{$keyword};
100 if (ref($class) || !defined($class) || !length($class)) {
101 my $display = defined($class) ? $class : 'undef';
102 confess "Invalid class name ($display)";
105 return 1 if is_class_loaded($class);
107 (my $file = "$class.pm") =~ s{::}{/}g;
109 eval { CORE::require($file) };
110 confess "Could not load class ($class) because : $@" if $@;
115 sub is_class_loaded {
118 return 0 if ref($class) || !defined($class) || !length($class);
120 # walk the symbol table tree to avoid autovififying
121 # \*{${main::}{"Foo::"}} == \*main::Foo::
124 foreach my $part (split('::', $class)) {
125 return 0 unless exists ${$$pack}{"${part}::"};
126 $pack = \*{${$$pack}{"${part}::"}};
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};
135 # check for any method
136 foreach ( keys %{$$pack} ) {
137 next if substr($_, -2, 2) eq '::';
138 return 1 if defined *{${$$pack}{$_}}{CODE};
151 Mouse - Moose minus the antlers
155 Version 0.02 released 11 Jun 08
160 use Mouse; # automatically turns on strict and warnings
162 has 'x' => (is => 'rw', isa => 'Int');
163 has 'y' => (is => 'rw', isa => 'Int');
176 has 'z' => (is => 'rw', isa => 'Int');
178 after 'clear' => sub {
185 L<Moose> is wonderful.
187 Unfortunately, it's a little slow. Though significant progress has been made
188 over the years, the compile time penalty is a non-starter for some
191 Mouse aims to alleviate this by providing a subset of Moose's
192 functionality, faster. In particular, L<Moose/has> is missing only a few
193 expert-level features.
197 Compatibility with Moose has been the utmost concern. Fewer than 1% of the
198 tests fail when run against Moose instead of Mouse. Mouse code coverage is also
199 over 99%. Even the error messages are taken from Moose. The Mouse code just
200 runs the test suite 3x-4x faster.
202 The idea is that, if you need the extra power, you should be able to run
203 C<s/Mouse/Moose/g> on your codebase and have nothing break. To that end,
204 nothingmuch has written L<Squirrel> (part of this distribution) which will act
205 as Mouse unless Moose is loaded, in which case it will act as Moose.
207 Mouse also has the blessings of Moose's author, stevan.
209 =head2 MISSING FEATURES
211 =head3 Method modifiers
213 Fixing this one next, with a reimplementation of L<Class::Method::Modifiers>.
217 Fixing this one slightly less soon. stevan has suggested an implementation
218 strategy. Mouse currently mostly ignores methods.
222 User-defined type constraints and parameterized types may be implemented. Type
223 coercions probably not (patches welcome).
225 =head3 Bootstrapped meta world
227 Very handy for extensions to the MOP. Not pressing, but would be nice to have.
229 =head3 Modification of attribute metaclass
231 When you declare an attribute with L</has>, you get the inlined accessors
232 installed immediately. Modifying the attribute metaclass, even if possible,
241 =head2 meta -> Mouse::Meta::Class
243 Returns this class' metaclass instance.
245 =head2 extends superclasses
247 Sets this class' superclasses.
249 =head2 has (name|names) => parameters
251 Adds an attribute (or if passed an arrayref of names, multiple attributes) to
258 If specified, inlines a read-only/read-write accessor with the same name as
261 =item isa => TypeConstraint
263 Provides basic type checking in the constructor and accessor. Basic types such
264 as C<Int>, C<ArrayRef>, C<Defined> are supported. Any unknown type is taken to
265 be a class check (e.g. isa => 'DateTime' would accept only L<DateTime>
268 =item required => 0|1
270 Whether this attribute is required to have a value. If the attribute is lazy or
271 has a builder, then providing a value for the attribute in the constructor is
274 =item init_arg => Str
276 Allows you to use a different key name in the constructor.
278 =item default => Value | CodeRef
280 Sets the default value of the attribute. If the default is a coderef, it will
281 be invoked to get the default value. Due to quirks of Perl, any bare reference
282 is forbidden, you must wrap the reference in a coderef. Otherwise, all
283 instances will share the same reference.
287 If specified, the default is calculated on demand instead of in the
290 =item predicate => Str
292 Lets you specify a method name for installing a predicate method, which checks
293 that the attribute has a value. It will not invoke a lazy default or builder
298 Lets you specify a method name for installing a clearer method, which clears
299 the attribute's value from the instance. On the next read, lazy or builder will
302 =item handles => HashRef|ArrayRef
304 Lets you specify methods to delegate to the attribute. ArrayRef forwards the
305 given method names to method calls on the attribute. HashRef maps local method
306 names to remote method names called on the attribute. Other forms of
307 L</handles>, such as regular expression and coderef, are not yet supported.
309 =item weak_ref => 0|1
311 Lets you automatically weaken any reference stored in the attribute.
313 =item trigger => Coderef
315 Any time the attribute's value is set (either through the accessor or the
316 constructor), the trigger is called on it. The trigger receives as arguments
317 the instance, the new value, and the attribute instance.
321 Defines a method name to be called to provide the default value of the
322 attribute. C<< builder => 'build_foo' >> is mostly equivalent to
323 C<< default => sub { $_[0]->build_foo } >>.
325 =item auto_deref => 0|1
327 Allows you to automatically dereference ArrayRef and HashRef attributes in list
328 context. In scalar context, the reference is returned (NOT the list length or
329 bucket status). You must specify an appropriate type constraint to use
334 =head2 confess error -> BOOM
336 L<Carp/confess> for your convenience.
338 =head2 blessed value -> ClassName | undef
340 L<Scalar::Util/blessed> for your convenience.
346 Importing Mouse will default your class' superclass list to L<Mouse::Object>.
347 You may use L</extends> to replace the superclass list.
351 Please unimport Mouse (C<no Mouse>) so that if someone calls one of the
352 keywords (such as L</extends>) it will break loudly instead breaking subtly.
356 =head2 load_class Class::Name
358 This will load a given C<Class::Name> (or die if it's not loadable).
359 This function can be used in place of tricks like
360 C<eval "use $module"> or using C<require>.
362 =head2 is_class_loaded Class::Name -> Bool
364 Returns whether this class is actually loaded or not. It uses a heuristic which
365 involves checking for the existence of C<$VERSION>, C<@ISA>, and any
366 locally-defined method.
370 Shawn M Moore, C<< <sartak at gmail.com> >>
372 with plenty of code borrowed from L<Class::MOP> and L<Moose>
378 Please report any bugs through RT: email
379 C<bug-mouse at rt.cpan.org>, or browse
380 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mouse>.
382 =head1 COPYRIGHT AND LICENSE
384 Copyright 2008 Shawn M Moore.
386 This program is free software; you can redistribute it and/or modify it
387 under the same terms as Perl itself.