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 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 #after 'clear' => sub {
175 =head2 meta -> Mouse::Meta::Class
177 Returns this class' metaclass instance.
179 =head2 extends superclasses
181 Sets this class' superclasses.
183 =head2 has (name|names) => parameters
185 Adds an attribute (or if passed an arrayref of names, multiple attributes) to
188 =head2 confess error -> BOOM
190 L<Carp/confess> for your convenience.
192 =head2 blessed value -> ClassName | undef
194 L<Scalar::Util/blessed> for your convenience.
200 Importing Mouse will default your class' superclass list to L<Mouse::Object>.
201 You may use L</extends> to replace the superclass list.
205 Please unimport Mouse so that if someone calls one of the keywords (such as
206 L</extends>) it will break loudly instead breaking subtly.
210 =head2 load_class Class::Name
212 This will load a given C<Class::Name> (or die if it's not loadable).
213 This function can be used in place of tricks like
214 C<eval "use $module"> or using C<require>.
216 =head2 is_class_loaded Class::Name -> Bool
218 Returns whether this class is actually loaded or not. It uses a heuristic which
219 involves checking for the existence of C<$VERSION>, C<@ISA>, and any
220 locally-defined method.
224 Shawn M Moore, C<< <sartak at gmail.com> >>
230 Please report any bugs through RT: email
231 C<bug-mouse at rt.cpan.org>, or browse
232 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mouse>.
234 =head1 COPYRIGHT AND LICENSE
236 Copyright 2008 Shawn M Moore.
238 This program is free software; you can redistribute it and/or modify it
239 under the same terms as Perl itself.