10 use constant _MOUSE_VERBOSE => !!$ENV{MOUSE_VERBOSE};
12 our @ISA = qw(Exporter);
16 resolve_metaclass_alias
35 meta => [qw(does meta dump _MOUSE_VERBOSE)],
38 # aliases as public APIs
40 # it must be 'require', not 'use', because Mouse::Meta::Module depends on Mouse::Util
41 require Mouse::Meta::Module; # for the entities of metaclass cache utilities
44 *class_of = \&Mouse::Meta::Module::class_of;
45 *get_metaclass_by_name = \&Mouse::Meta::Module::get_metaclass_by_name;
46 *get_all_metaclass_instances = \&Mouse::Meta::Module::get_all_metaclass_instances;
47 *get_all_metaclass_names = \&Mouse::Meta::Module::get_all_metaclass_names;
50 # Moose::Util compatible utilities
53 return class_of( $_[0] );
57 my ($class_or_obj, $role_name) = @_;
59 my $meta = class_of($class_or_obj);
62 || ($meta || 'Mouse::Meta::Class')->throw_error("You must supply a role name to does()");
64 return defined($meta) && $meta->does_role($role_name);
69 if ($] >= 5.009_005) {
71 $impl = \&mro::get_linear_isa;
75 eval { require MRO::Compat };
79 $impl = \&mro::get_linear_isa;
81 # VVVVV CODE TAKEN FROM MRO::COMPAT VVVVV
82 my $_get_linear_isa_dfs; # this recurses so it isn't pretty
83 $_get_linear_isa_dfs = sub {
86 my $classname = shift;
88 my @lin = ($classname);
90 foreach my $parent (@{"$classname\::ISA"}) {
91 my $plin = $_get_linear_isa_dfs->($parent);
92 foreach my $p(@$plin) {
93 next if exists $stored{$p};
100 # ^^^^^ CODE TAKEN FROM MRO::COMPAT ^^^^^
101 $impl = $_get_linear_isa_dfs;
107 *get_linear_isa = $impl;
110 { # taken from Sub::Identify
111 sub get_code_info($) {
113 ref($coderef) or return;
115 my $cv = B::svref_2object($coderef);
116 $cv->isa('B::CV') or return;
119 $gv->isa('B::GV') or return;
121 return ($gv->STASH->NAME, $gv->NAME);
124 sub get_code_package{
127 my $cv = B::svref_2object($coderef);
128 $cv->isa('B::CV') or return '';
131 $gv->isa('B::GV') or return '';
133 return $gv->STASH->NAME;
137 # taken from Mouse::Util (0.90)
141 sub resolve_metaclass_alias {
142 my ( $type, $metaclass_name, %options ) = @_;
144 my $cache_key = $type . q{ } . ( $options{trait} ? '-Trait' : '' );
146 return $cache{$cache_key}{$metaclass_name} ||= do{
148 my $possible_full_name = join '::',
149 'Mouse::Meta', $type, 'Custom', ($options{trait} ? 'Trait' : ()), $metaclass_name
152 my $loaded_class = load_first_existing_class(
157 $loaded_class->can('register_implementation')
158 ? $loaded_class->register_implementation
164 # Utilities from Class::MOP
167 # taken from Class/MOP.pm
168 sub is_valid_class_name {
171 return 0 if ref($class);
172 return 0 unless defined($class);
174 return 1 if $class =~ /^\w+(?:::\w+)*$/;
179 # taken from Class/MOP.pm
180 sub load_first_existing_class {
185 for my $class (@classes) {
186 my $e = _try_load_one_class($class);
189 $exceptions{$class} = $e;
200 sprintf( "Could not load class (%s) because : %s",
201 $_, $exceptions{$_} )
206 # taken from Class/MOP.pm
207 my %is_class_loaded_cache;
208 sub _try_load_one_class {
211 unless ( is_valid_class_name($class) ) {
212 my $display = defined($class) ? $class : 'undef';
213 confess "Invalid class name ($display)";
216 return undef if $is_class_loaded_cache{$class} ||= is_class_loaded($class);
218 my $file = $class . '.pm';
223 eval { require($file) };
231 my $e = _try_load_one_class($class);
232 confess "Could not load class ($class) because : $e" if $e;
238 sub is_class_loaded {
241 return 0 if ref($class) || !defined($class) || !length($class);
243 # walk the symbol table tree to avoid autovififying
244 # \*{${main::}{"Foo::"}} == \*main::Foo::
247 foreach my $part (split('::', $class)) {
248 my $entry = \$pack->{$part . '::'};
249 return 0 if ref($entry) ne 'GLOB';
250 $pack = *{$entry}{HASH} or return 0;
253 # check for $VERSION or @ISA
254 return 1 if exists $pack->{VERSION}
255 && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} };
256 return 1 if exists $pack->{ISA}
257 && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0;
259 # check for any method
260 foreach my $name( keys %{$pack} ) {
261 my $entry = \$pack->{$name};
262 return 1 if ref($entry) ne 'GLOB' || defined *{$entry}{CODE};
270 sub apply_all_roles {
271 my $meta = Mouse::Meta::Class->initialize(shift);
275 # Basis of Data::OptList
276 my $max = scalar(@_);
277 for (my $i = 0; $i < $max ; $i++) {
278 if ($i + 1 < $max && ref($_[$i + 1])) {
279 push @roles, [ $_[$i++] => $_[$i] ];
281 push @roles, [ $_[$i] => undef ];
283 my $role_name = $roles[-1][0];
284 load_class($role_name);
285 ( $role_name->can('meta') && $role_name->meta->isa('Mouse::Meta::Role') )
286 || $meta->throw_error("You can only consume roles, $role_name(".$role_name->meta.") is not a Mouse role");
289 if ( scalar @roles == 1 ) {
290 my ( $role, $params ) = @{ $roles[0] };
291 $role->meta->apply( $meta, ( defined $params ? %$params : () ) );
294 Mouse::Meta::Role->combine_apply($meta, @roles);
299 # taken from Moose::Util 0.90
301 return $_[0] if @_ == 1;
305 return "$items[0] and $items[1]" if @items == 2;
307 my $tail = pop @items;
309 return join q{, }, @items, "and $tail";
318 $feature ||= ( caller(1) )[3]; # subroutine name
320 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
321 Carp::confess("Mouse does not currently support $feature");
325 return Mouse::Meta::Class->initialize(ref($_[0]) || $_[0]);
329 my($self, $maxdepth) = @_;
331 require 'Data/Dumper.pm'; # we don't want to create its namespace
332 my $dd = Data::Dumper->new([$self]);
333 $dd->Maxdepth(defined($maxdepth) ? $maxdepth : 2);
339 *does = \&does_role; # alias
347 Mouse::Util - Features, with or without their dependencies
349 =head1 IMPLEMENTATIONS FOR
357 =head3 C<resolve_metaclass_alias>
359 =head3 C<apply_all_roles>
361 =head3 C<english_list>
365 =head3 C<< is_class_loaded(ClassName) -> Bool >>
367 Returns whether C<ClassName> is actually loaded or not. It uses a heuristic which
368 involves checking for the existence of C<$VERSION>, C<@ISA>, and any
369 locally-defined method.
371 =head3 C<< load_class(ClassName) >>
373 This will load a given C<ClassName> (or die if it is not loadable).
374 This function can be used in place of tricks like
375 C<eval "use $module"> or using C<require>.
377 =head3 C<< Mouse::Util::class_of(ClassName or Object) >>
379 =head3 C<< Mouse::Util::get_metaclass_by_name(ClassName) >>
381 =head3 C<< Mouse::Util::get_all_metaclass_instances() >>
383 =head3 C<< Mouse::Util::get_all_metaclass_names() >>
387 =head3 C<get_linear_isa>
391 =head3 C<get_code_info>
393 =head1 UTILITIES FOR MOUSE
395 =head3 C<not_supported>