11 use Scalar::Util 'blessed';
13 use Mouse::Meta::Attribute;
14 use Mouse::Meta::Class;
16 use Mouse::TypeRegistry;
23 my $meta = Mouse::Meta::Class->initialize($CALLER);
30 $caller->meta->superclasses(@_);
38 $names = [$names] if !ref($names);
40 for my $name (@$names) {
41 Mouse::Meta::Attribute->create($package, $name, @_);
55 my $exporter = Sub::Exporter::build_exporter({
57 groups => { default => [':all'] },
66 my $meta = Mouse::Meta::Class->initialize($CALLER);
67 $meta->superclasses('Mouse::Object')
68 unless $meta->superclasses;
77 for my $keyword (keys %exports) {
78 next if $keyword eq 'meta'; # we don't delete this one
79 delete ${ $caller . '::' }{$keyword};
87 if (ref($class) || !defined($class) || !length($class)) {
88 my $display = defined($class) ? $class : 'undef';
89 confess "Invalid class name ($display)";
92 return 1 if is_class_loaded($class);
94 (my $file = "$class.pm") =~ s{::}{/}g;
96 eval { CORE::require($file) };
97 confess "Could not load class ($class) because : $@" if $@;
102 sub is_class_loaded {
105 return 0 if ref($class) || !defined($class) || !length($class);
107 # walk the symbol table tree to avoid autovififying
108 # \*{${main::}{"Foo::"}} == \*main::Foo::
111 foreach my $part (split('::', $class)) {
112 return 0 unless exists ${$$pack}{"${part}::"};
113 $pack = \*{${$$pack}{"${part}::"}};
116 # check for $VERSION or @ISA
117 return 1 if exists ${$$pack}{VERSION}
118 && defined *{${$$pack}{VERSION}}{SCALAR};
119 return 1 if exists ${$$pack}{ISA}
120 && defined *{${$$pack}{ISA}}{ARRAY};
122 # check for any method
123 foreach ( keys %{$$pack} ) {
124 next if substr($_, -2, 2) eq '::';
125 return 1 if defined *{${$$pack}{$_}}{CODE};
138 Mouse - Moose minus the antlers
142 Version 0.01 released 10 Jun 08
147 use Mouse; # automatically turns on strict and warnings
149 has 'x' => (is => 'rw', isa => 'Int');
150 has 'y' => (is => 'rw', isa => 'Int');
163 has 'z' => (is => 'rw', isa => 'Int');
165 # not implemented yet :)
166 #after 'clear' => sub {
173 L<Moose> is wonderful.
175 Unfortunately, it's a little slow. Though significant progress has been made
176 over the years, the compile time penalty is a non-starter for some
179 Mouse aims to alleviate this by providing a subset of Moose's
180 functionality, faster. In particular, L<Moose/has> is missing only a few
181 expert-level features.
185 Compatibility with Moose has been the utmost concern. Fewer than 1% of the
186 tests fail when run against Moose instead of Mouse. Mouse code coverage is also
187 over 99%. Even the error messages are taken from Moose.
189 The idea is that, if you need the extra power, you should be able to run
190 C<s/Mouse/Moose/g> on your codebase and have nothing break.
192 Mouse also has the blessings of Moose's author, stevan.
194 =head2 MISSING FEATURES
196 =head3 Method modifiers
198 Fixing this one next, with a reimplementation of L<Class::Method::Modifiers>.
202 Fixing this one slightly less soon. stevan has suggested an implementation
203 strategy. Mouse currently mostly ignores methods.
207 User-defined type constraints and parameterized types may be implemented. Type
208 coercions probably not (patches welcome).
210 =head3 Bootstrapped meta world
212 Very handy for extensions to the MOP. Not pressing, but would be nice to have.
214 =head3 Modification of attribute metaclass
216 When you declare an attribute with L</has>, you get the inlined accessors
217 installed immediately. Modifying the attribute metaclass, even if possible,
226 =head2 meta -> Mouse::Meta::Class
228 Returns this class' metaclass instance.
230 =head2 extends superclasses
232 Sets this class' superclasses.
234 =head2 has (name|names) => parameters
236 Adds an attribute (or if passed an arrayref of names, multiple attributes) to
243 If specified, inlines a read-only/read-write accessor with the same name as
246 =item isa => TypeConstraint
248 Provides basic type checking in the constructor and accessor. Basic types such
249 as C<Int>, C<ArrayRef>, C<Defined> are supported. Any unknown type is taken to
250 be a class check (e.g. isa => 'DateTime' would accept only L<DateTime>
253 =item required => 0|1
255 Whether this attribute is required to have a value. If the attribute is lazy or
256 has a builder, then providing a value for the attribute in the constructor is
259 =item init_arg => Str
261 Allows you to use a different key name in the constructor.
263 =item default => Value | CodeRef
265 Sets the default value of the attribute. If the default is a coderef, it will
266 be invoked to get the default value. Due to quirks of Perl, any bare reference
267 is forbidden, you must wrap the reference in a coderef. Otherwise, all
268 instances will share the same reference.
272 If specified, the default is calculated on demand instead of in the
275 =item predicate => Str
277 Lets you specify a method name for installing a predicate method, which checks
278 that the attribute has a value. It will not invoke a lazy default or builder
283 Lets you specify a method name for installing a clearer method, which clears
284 the attribute's value from the instance. On the next read, lazy or builder will
287 =item handles => HashRef|ArrayRef
289 Lets you specify methods to delegate to the attribute. ArrayRef forwards the
290 given method names to method calls on the attribute. HashRef maps local method
291 names to remote method names called on the attribute. Other forms of
292 L</handles>, such as regular expression and coderef, are not yet supported.
294 =item weak_ref => 0|1
296 Lets you automatically weaken any reference stored in the attribute.
298 =item trigger => Coderef
300 Any time the attribute's value is set (either through the accessor or the
301 constructor), the trigger is called on it. The trigger receives as arguments
302 the instance, the new value, and the attribute instance.
306 Defines a method name to be called to provide the default value of the
307 attribute. C<< builder => 'build_foo' >> is mostly equivalent to
308 C<< default => sub { $_[0]->build_foo } >>.
310 =item auto_deref => 0|1
312 Allows you to automatically dereference ArrayRef and HashRef attributes in list
313 context. In scalar context, the reference is returned (NOT the list length or
314 bucket status). You must specify an appropriate type constraint to use
319 =head2 confess error -> BOOM
321 L<Carp/confess> for your convenience.
323 =head2 blessed value -> ClassName | undef
325 L<Scalar::Util/blessed> for your convenience.
331 Importing Mouse will default your class' superclass list to L<Mouse::Object>.
332 You may use L</extends> to replace the superclass list.
336 Please unimport Mouse (C<no Mouse>) so that if someone calls one of the
337 keywords (such as L</extends>) it will break loudly instead breaking subtly.
341 =head2 load_class Class::Name
343 This will load a given C<Class::Name> (or die if it's not loadable).
344 This function can be used in place of tricks like
345 C<eval "use $module"> or using C<require>.
347 =head2 is_class_loaded Class::Name -> Bool
349 Returns whether this class is actually loaded or not. It uses a heuristic which
350 involves checking for the existence of C<$VERSION>, C<@ISA>, and any
351 locally-defined method.
355 Shawn M Moore, C<< <sartak at gmail.com> >>
357 with plenty of code borrowed from L<Class::MOP> and L<Moose>
363 Please report any bugs through RT: email
364 C<bug-mouse at rt.cpan.org>, or browse
365 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mouse>.
367 =head1 COPYRIGHT AND LICENSE
369 Copyright 2008 Shawn M Moore.
371 This program is free software; you can redistribute it and/or modify it
372 under the same terms as Perl itself.