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 # Moose::Util compatible utilities
41 return Mouse::Meta::Module::class_of( $_[0] );
45 my ($class_or_obj, $role_name) = @_;
47 my $meta = Mouse::Meta::Module::class_of($class_or_obj);
50 || ($meta || 'Mouse::Meta::Class')->throw_error("You must supply a role name to does()");
52 return defined($meta) && $meta->does_role($role_name);
59 if ($] >= 5.009_005) {
61 $impl = \&mro::get_linear_isa;
65 eval { require MRO::Compat };
69 $impl = \&mro::get_linear_isa;
71 # VVVVV CODE TAKEN FROM MRO::COMPAT VVVVV
72 my $_get_linear_isa_dfs; # this recurses so it isn't pretty
73 $_get_linear_isa_dfs = sub {
76 my $classname = shift;
78 my @lin = ($classname);
80 foreach my $parent (@{"$classname\::ISA"}) {
81 my $plin = $_get_linear_isa_dfs->($parent);
82 foreach my $p(@$plin) {
83 next if exists $stored{$p};
90 # ^^^^^ CODE TAKEN FROM MRO::COMPAT ^^^^^
91 $impl = $_get_linear_isa_dfs;
97 *get_linear_isa = $impl;
100 { # taken from Sub::Identify
101 sub get_code_info($) {
103 ref($coderef) or return;
105 my $cv = B::svref_2object($coderef);
106 $cv->isa('B::CV') or return;
109 $gv->isa('B::GV') or return;
111 return ($gv->STASH->NAME, $gv->NAME);
114 sub get_code_package{
117 my $cv = B::svref_2object($coderef);
118 $cv->isa('B::CV') or return '';
121 $gv->isa('B::GV') or return '';
123 return $gv->STASH->NAME;
127 # taken from Mouse::Util (0.90)
131 sub resolve_metaclass_alias {
132 my ( $type, $metaclass_name, %options ) = @_;
134 my $cache_key = $type . q{ } . ( $options{trait} ? '-Trait' : '' );
136 return $cache{$cache_key}{$metaclass_name} ||= do{
138 my $possible_full_name = join '::',
139 'Mouse::Meta', $type, 'Custom', ($options{trait} ? 'Trait' : ()), $metaclass_name
142 my $loaded_class = load_first_existing_class(
147 $loaded_class->can('register_implementation')
148 ? $loaded_class->register_implementation
154 # taken from Class/MOP.pm
155 sub is_valid_class_name {
158 return 0 if ref($class);
159 return 0 unless defined($class);
161 return 1 if $class =~ /^\w+(?:::\w+)*$/;
166 # taken from Class/MOP.pm
167 sub load_first_existing_class {
172 for my $class (@classes) {
173 my $e = _try_load_one_class($class);
176 $exceptions{$class} = $e;
187 sprintf( "Could not load class (%s) because : %s",
188 $_, $exceptions{$_} )
193 # taken from Class/MOP.pm
194 sub _try_load_one_class {
197 unless ( is_valid_class_name($class) ) {
198 my $display = defined($class) ? $class : 'undef';
199 confess "Invalid class name ($display)";
202 return if is_class_loaded($class);
204 my $file = $class . '.pm';
209 eval { require($file) };
217 my $e = _try_load_one_class($class);
218 confess "Could not load class ($class) because : $e" if $e;
223 my %is_class_loaded_cache;
224 sub is_class_loaded {
227 return 0 if ref($class) || !defined($class) || !length($class);
229 return 1 if $is_class_loaded_cache{$class};
231 # walk the symbol table tree to avoid autovififying
232 # \*{${main::}{"Foo::"}} == \*main::Foo::
235 foreach my $part (split('::', $class)) {
236 my $entry = \$pack->{$part . '::'};
237 return 0 if ref($entry) ne 'GLOB';
238 $pack = *{$entry}{HASH} or return 0;
241 # check for $VERSION or @ISA
242 return ++$is_class_loaded_cache{$class} if exists $pack->{VERSION}
243 && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} };
244 return ++$is_class_loaded_cache{$class} if exists $pack->{ISA}
245 && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0;
247 # check for any method
248 foreach my $name( keys %{$pack} ) {
249 my $entry = \$pack->{$name};
250 return ++$is_class_loaded_cache{$class} if ref($entry) ne 'GLOB' || defined *{$entry}{CODE};
258 sub apply_all_roles {
259 my $meta = Mouse::Meta::Class->initialize(shift);
263 # Basis of Data::OptList
264 my $max = scalar(@_);
265 for (my $i = 0; $i < $max ; $i++) {
266 if ($i + 1 < $max && ref($_[$i + 1])) {
267 push @roles, [ $_[$i++] => $_[$i] ];
269 push @roles, [ $_[$i] => undef ];
271 my $role_name = $roles[-1][0];
272 load_class($role_name);
273 ( $role_name->can('meta') && $role_name->meta->isa('Mouse::Meta::Role') )
274 || $meta->throw_error("You can only consume roles, $role_name(".$role_name->meta.") is not a Mouse role");
277 if ( scalar @roles == 1 ) {
278 my ( $role, $params ) = @{ $roles[0] };
279 $role->meta->apply( $meta, ( defined $params ? %$params : () ) );
282 Mouse::Meta::Role->combine_apply($meta, @roles);
287 # taken from Moose::Util 0.90
289 return $_[0] if @_ == 1;
293 return "$items[0] and $items[1]" if @items == 2;
295 my $tail = pop @items;
297 return join q{, }, @items, "and $tail";
306 $feature ||= ( caller(1) )[3]; # subroutine name
308 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
309 Carp::confess("Mouse does not currently support $feature");
313 return Mouse::Meta::Class->initialize($_[0]);
317 my($self, $maxdepth) = @_;
319 require 'Data/Dumper.pm'; # we don't want to create its namespace
320 my $dd = Data::Dumper->new([$self]);
321 $dd->Maxdepth(defined($maxdepth) ? $maxdepth : 2);
327 *does = \&does_role; # alias
335 Mouse::Util - Features, with or without their dependencies
337 =head1 IMPLEMENTATIONS FOR
345 =head3 C<resolve_metaclass_alias>
347 =head3 C<apply_all_roles>
349 =head3 C<english_list>
353 =head3 C<< is_class_loaded(ClassName) -> Bool >>
355 Returns whether C<ClassName> is actually loaded or not. It uses a heuristic which
356 involves checking for the existence of C<$VERSION>, C<@ISA>, and any
357 locally-defined method.
359 =head3 C<< load_class(ClassName) >>
361 This will load a given C<ClassName> (or die if it's not loadable).
362 This function can be used in place of tricks like
363 C<eval "use $module"> or using C<require>.
367 =head3 C<get_linear_isa>
371 =head3 C<get_code_info>
373 =head1 UTILITIES FOR MOUSE
375 =head3 C<not_supported>