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 confess "Mouse::Role only supports 'with' on individual roles at a time" if @_;
81 Mouse::load_class($role);
82 $role->meta->apply($class);
87 my $exporter = Sub::Exporter::build_exporter({
89 groups => { default => [':all'] },
98 my $meta = Mouse::Meta::Class->initialize($CALLER);
99 $meta->superclasses('Mouse::Object')
100 unless $meta->superclasses;
109 for my $keyword (keys %exports) {
110 next if $keyword eq 'meta'; # we don't delete this one
111 delete ${ $caller . '::' }{$keyword};
119 if (ref($class) || !defined($class) || !length($class)) {
120 my $display = defined($class) ? $class : 'undef';
121 confess "Invalid class name ($display)";
124 return 1 if is_class_loaded($class);
126 (my $file = "$class.pm") =~ s{::}{/}g;
128 eval { CORE::require($file) };
129 confess "Could not load class ($class) because : $@" if $@;
134 sub is_class_loaded {
137 return 0 if ref($class) || !defined($class) || !length($class);
139 # walk the symbol table tree to avoid autovififying
140 # \*{${main::}{"Foo::"}} == \*main::Foo::
143 foreach my $part (split('::', $class)) {
144 return 0 unless exists ${$$pack}{"${part}::"};
145 $pack = \*{${$$pack}{"${part}::"}};
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};
154 # check for any method
155 foreach ( keys %{$$pack} ) {
156 next if substr($_, -2, 2) eq '::';
157 return 1 if defined *{${$$pack}{$_}}{CODE};
170 Mouse - Moose minus the antlers
175 use Mouse; # automatically turns on strict and warnings
177 has 'x' => (is => 'rw', isa => 'Int');
178 has 'y' => (is => 'rw', isa => 'Int');
191 has 'z' => (is => 'rw', isa => 'Int');
193 after 'clear' => sub {
200 L<Moose> is wonderful.
202 Unfortunately, it's a little slow. Though significant progress has been made
203 over the years, the compile time penalty is a non-starter for some
206 Mouse aims to alleviate this by providing a subset of Moose's
207 functionality, faster. In particular, L<Moose/has> is missing only a few
208 expert-level features.
212 Compatibility with Moose has been the utmost concern. Fewer than 1% of the
213 tests fail when run against Moose instead of Mouse. Mouse code coverage is also
214 over 99%. Even the error messages are taken from Moose. The Mouse code just
215 runs the test suite 3x-4x faster.
217 The idea is that, if you need the extra power, you should be able to run
218 C<s/Mouse/Moose/g> on your codebase and have nothing break. To that end,
219 nothingmuch has written L<Squirrel> (part of this distribution) which will act
220 as Mouse unless Moose is loaded, in which case it will act as Moose.
222 Mouse also has the blessings of Moose's author, stevan.
224 =head2 MISSING FEATURES
228 Fixing this one slightly less soon. stevan has suggested an implementation
229 strategy. Mouse currently mostly ignores methods.
233 User-defined type constraints and parameterized types may be implemented. Type
234 coercions probably not (patches welcome).
236 =head3 Bootstrapped meta world
238 Very handy for extensions to the MOP. Not pressing, but would be nice to have.
240 =head3 Modification of attribute metaclass
242 When you declare an attribute with L</has>, you get the inlined accessors
243 installed immediately. Modifying the attribute metaclass, even if possible,
252 =head2 meta -> Mouse::Meta::Class
254 Returns this class' metaclass instance.
256 =head2 extends superclasses
258 Sets this class' superclasses.
260 =head2 before (method|methods) => Code
262 Installs a "before" method modifier. See L<Moose/before> or
263 L<Class::Method::Modifiers/before>.
265 =head2 after (method|methods) => Code
267 Installs an "after" method modifier. See L<Moose/after> or
268 L<Class::Method::Modifiers/after>.
270 =head2 around (method|methods) => Code
272 Installs an "around" method modifier. See L<Moose/around> or
273 L<Class::Method::Modifiers/around>.
275 =head2 has (name|names) => parameters
277 Adds an attribute (or if passed an arrayref of names, multiple attributes) to
284 If specified, inlines a read-only/read-write accessor with the same name as
287 =item isa => TypeConstraint
289 Provides basic type checking in the constructor and accessor. Basic types such
290 as C<Int>, C<ArrayRef>, C<Defined> are supported. Any unknown type is taken to
291 be a class check (e.g. isa => 'DateTime' would accept only L<DateTime>
294 =item required => 0|1
296 Whether this attribute is required to have a value. If the attribute is lazy or
297 has a builder, then providing a value for the attribute in the constructor is
300 =item init_arg => Str
302 Allows you to use a different key name in the constructor.
304 =item default => Value | CodeRef
306 Sets the default value of the attribute. If the default is a coderef, it will
307 be invoked to get the default value. Due to quirks of Perl, any bare reference
308 is forbidden, you must wrap the reference in a coderef. Otherwise, all
309 instances will share the same reference.
313 If specified, the default is calculated on demand instead of in the
316 =item predicate => Str
318 Lets you specify a method name for installing a predicate method, which checks
319 that the attribute has a value. It will not invoke a lazy default or builder
324 Lets you specify a method name for installing a clearer method, which clears
325 the attribute's value from the instance. On the next read, lazy or builder will
328 =item handles => HashRef|ArrayRef
330 Lets you specify methods to delegate to the attribute. ArrayRef forwards the
331 given method names to method calls on the attribute. HashRef maps local method
332 names to remote method names called on the attribute. Other forms of
333 L</handles>, such as regular expression and coderef, are not yet supported.
335 =item weak_ref => 0|1
337 Lets you automatically weaken any reference stored in the attribute.
339 =item trigger => CodeRef | HashRef
341 Triggers are like method modifiers for setting attribute values. You can have
342 a "before" and an "after" trigger, each of which receive as arguments the instance, the new value, and the attribute metaclass. Historically, triggers have
343 only been "after" modifiers, so if you use a coderef for the C<trigger> option,
344 it will maintain that compatibility. Like method modifiers, you can't really
345 affect the act of setting the attribute value, and the return values of the
346 modifiers are ignored.
348 There's also an "around" trigger which you can use to change the value that
349 is being set on the attribute, or even prevent the attribute from being
350 updated. The around trigger receives as arguments a code reference to invoke
351 to set the attribute's value (which expects as arguments the instance and
352 the new value), the instance, the new value, and the attribute metaclass.
356 Defines a method name to be called to provide the default value of the
357 attribute. C<< builder => 'build_foo' >> is mostly equivalent to
358 C<< default => sub { $_[0]->build_foo } >>.
360 =item auto_deref => 0|1
362 Allows you to automatically dereference ArrayRef and HashRef attributes in list
363 context. In scalar context, the reference is returned (NOT the list length or
364 bucket status). You must specify an appropriate type constraint to use
369 =head2 confess error -> BOOM
371 L<Carp/confess> for your convenience.
373 =head2 blessed value -> ClassName | undef
375 L<Scalar::Util/blessed> for your convenience.
381 Importing Mouse will default your class' superclass list to L<Mouse::Object>.
382 You may use L</extends> to replace the superclass list.
386 Please unimport Mouse (C<no Mouse>) so that if someone calls one of the
387 keywords (such as L</extends>) it will break loudly instead breaking subtly.
391 =head2 load_class Class::Name
393 This will load a given C<Class::Name> (or die if it's not loadable).
394 This function can be used in place of tricks like
395 C<eval "use $module"> or using C<require>.
397 =head2 is_class_loaded Class::Name -> Bool
399 Returns whether this class is actually loaded or not. It uses a heuristic which
400 involves checking for the existence of C<$VERSION>, C<@ISA>, and any
401 locally-defined method.
405 Shawn M Moore, C<< <sartak at gmail.com> >>
407 with plenty of code borrowed from L<Class::MOP> and L<Moose>
413 Please report any bugs through RT: email
414 C<bug-mouse at rt.cpan.org>, or browse
415 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mouse>.
417 =head1 COPYRIGHT AND LICENSE
419 Copyright 2008 Shawn M Moore.
421 This program is free software; you can redistribute it and/or modify it
422 under the same terms as Perl itself.