10 use Scalar::Util 'blessed';
12 use Mouse::Meta::Attribute;
13 use Mouse::Meta::Class;
15 use Mouse::TypeRegistry;
22 my $meta = Mouse::Meta::Class->initialize($CALLER);
29 $caller->meta->superclasses(@_);
37 $names = [$names] if !ref($names);
39 for my $name (@$names) {
40 Mouse::Meta::Attribute->create($package, $name, @_);
54 my $exporter = Sub::Exporter::build_exporter({
56 groups => { default => [':all'] },
65 my $meta = Mouse::Meta::Class->initialize($CALLER);
66 $meta->superclasses('Mouse::Object')
67 unless $meta->superclasses;
76 for my $keyword (keys %exports) {
77 next if $keyword eq 'meta'; # we don't delete this one
78 delete ${ $caller . '::' }{$keyword};
86 if (ref($class) || !defined($class) || !length($class)) {
87 my $display = defined($class) ? $class : 'undef';
88 confess "Invalid class name ($display)";
91 return 1 if is_class_loaded($class);
93 (my $file = "$class.pm") =~ s{::}{/}g;
95 eval { CORE::require($file) };
96 confess "Could not load class ($class) because : $@" if $@;
101 sub is_class_loaded {
104 return 0 if ref($class) || !defined($class) || !length($class);
106 # walk the symbol table tree to avoid autovififying
107 # \*{${main::}{"Foo::"}} == \*main::Foo::
110 foreach my $part (split('::', $class)) {
111 return 0 unless exists ${$$pack}{"${part}::"};
112 $pack = \*{${$$pack}{"${part}::"}};
115 # check for $VERSION or @ISA
116 return 1 if exists ${$$pack}{VERSION}
117 && defined *{${$$pack}{VERSION}}{SCALAR};
118 return 1 if exists ${$$pack}{ISA}
119 && defined *{${$$pack}{ISA}}{ARRAY};
121 # check for any method
122 foreach ( keys %{$$pack} ) {
123 next if substr($_, -2, 2) eq '::';
124 return 1 if defined *{${$$pack}{$_}}{CODE};
137 Mouse - Moose minus the antlers
141 Version 0.01 released ???
146 use Mouse; # automatically turns on strict and warnings
148 has 'x' => (is => 'rw', isa => 'Int');
149 has 'y' => (is => 'rw', isa => 'Int');
162 has 'z' => (is => 'rw', isa => 'Int');
164 # not implemented yet :)
165 #after 'clear' => sub {
172 L<Moose> is wonderful.
174 Unfortunately, it's a little slow. Though significant progress has been made
175 over the years, the compile time penalty is a non-starter for some
178 Mouse aims to alleviate this by providing a subset of Moose's
179 functionality, faster. In particular, L<Moose/has> is missing only a few
180 expert-level features.
184 Compatibility with Moose has been the utmost concern. Fewer than 1% of the
185 tests fail when run against Moose instead of Mouse. Mouse code coverage is also
186 over 99%. Even the error messages are taken from Moose.
188 The idea is that, if you need the extra power, you should be able to run
189 C<s/Mouse/Moose/g> on your codebase and have nothing break.
191 Mouse also has the blessings of Moose's author, stevan.
193 =head2 MISSING FEATURES
195 =head3 Method modifiers
197 Fixing this one soon, with a reimplementation of L<Class::Method::Modifiers>.
201 Fixing this one slightly less soon. stevan has suggested an implementation
202 strategy. Mouse currently mostly ignores methods.
206 User-defined type constraints and parameterized types may be implemented. Type
207 coercions probably not (patches welcome).
209 =head3 Bootstrapped meta world
211 Very handy for extensions to the MOP. Not pressing, but would be nice to have.
213 =head3 Modification of attribute metaclass
215 When you declare an attribute with L</has>, you get the inlined accessors
216 installed immediately. Modifying the attribute metaclass, even if possible,
225 =head2 meta -> Mouse::Meta::Class
227 Returns this class' metaclass instance.
229 =head2 extends superclasses
231 Sets this class' superclasses.
233 =head2 has (name|names) => parameters
235 Adds an attribute (or if passed an arrayref of names, multiple attributes) to
242 If specified, inlines a read-only/read-write accessor with the same name as
245 =item isa => TypeConstraint
247 Provides basic type checking in the constructor and accessor. Basic types such
248 as C<Int>, C<ArrayRef>, C<Defined> are supported. Any unknown type is taken to
249 be a class check (e.g. isa => 'DateTime' would accept only L<DateTime>
252 =item required => 0|1
254 Whether this attribute is required to have a value. If the attribute is lazy or
255 has a builder, then providing a value for the attribute in the constructor is
258 =item init_arg => Str
260 Allows you to use a different key name in the constructor.
262 =item default => Value | CodeRef
264 Sets the default value of the attribute. If the default is a coderef, it will
265 be invoked to get the default value. Due to quirks of Perl, any bare reference
266 is forbidden, you must wrap the reference in a coderef. Otherwise, all
267 instances will share the same reference.
271 If specified, the default is calculated on demand instead of in the
274 =item predicate => Str
276 Lets you specify a method name for installing a predicate method, which checks
277 that the attribute has a value. It will not invoke a lazy default or builder
282 Lets you specify a method name for installing a clearer method, which clears
283 the attribute's value from the instance. On the next read, lazy or builder will
286 =item handles => HashRef|ArrayRef
288 Lets you specify methods to delegate to the attribute. ArrayRef forwards the
289 given method names to method calls on the attribute. HashRef maps local method
290 names to remote method names called on the attribute. Other forms of
291 L</handles>, such as regular expression and coderef, are not yet supported.
293 =item weak_ref => 0|1
295 Lets you automatically weaken any reference stored in the attribute.
297 =item trigger => Coderef
299 Any time the attribute's value is set (either through the accessor or the
300 constructor), the trigger is called on it. The trigger receives as arguments
301 the instance, the new value, and the attribute instance.
305 Defines a method name to be called to provide the default value of the
306 attribute. C<< builder => 'build_foo' >> is mostly equivalent to
307 C<< default => sub { $_[0]->build_foo } >>.
309 =item auto_deref => 0|1
311 Allows you to automatically dereference ArrayRef and HashRef attributes in list
312 context. In scalar context, the reference is returned (NOT the list length or
313 bucket status). You must specify an appropriate type constraint to use
318 =head2 confess error -> BOOM
320 L<Carp/confess> for your convenience.
322 =head2 blessed value -> ClassName | undef
324 L<Scalar::Util/blessed> for your convenience.
330 Importing Mouse will default your class' superclass list to L<Mouse::Object>.
331 You may use L</extends> to replace the superclass list.
335 Please unimport Mouse (C<no Mouse>) so that if someone calls one of the
336 keywords (such as L</extends>) it will break loudly instead breaking subtly.
340 =head2 load_class Class::Name
342 This will load a given C<Class::Name> (or die if it's not loadable).
343 This function can be used in place of tricks like
344 C<eval "use $module"> or using C<require>.
346 =head2 is_class_loaded Class::Name -> Bool
348 Returns whether this class is actually loaded or not. It uses a heuristic which
349 involves checking for the existence of C<$VERSION>, C<@ISA>, and any
350 locally-defined method.
354 Shawn M Moore, C<< <sartak at gmail.com> >>
356 with plenty of code borrowed from L<Class::MOP> and L<Moose>
362 Please report any bugs through RT: email
363 C<bug-mouse at rt.cpan.org>, or browse
364 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mouse>.
366 =head1 COPYRIGHT AND LICENSE
368 Copyright 2008 Shawn M Moore.
370 This program is free software; you can redistribute it and/or modify it
371 under the same terms as Perl itself.