9 use constant _MOUSE_VERBOSE => !!$ENV{MOUSE_VERBOSE};
11 our @ISA = qw(Exporter);
15 resolve_metaclass_alias
32 meta => [qw(does meta dump _MOUSE_VERBOSE)],
35 # Moose::Util compatible utilities
38 return Mouse::Meta::Module::class_of( $_[0] );
42 my ($class_or_obj, $role_name) = @_;
44 my $meta = Mouse::Meta::Module::class_of($class_or_obj);
47 || ($meta || 'Mouse::Meta::Class')->throw_error("You must supply a role name to does()");
49 return defined($meta) && $meta->does_role($role_name);
56 if ($] >= 5.009_005) {
58 $impl = \&mro::get_linear_isa;
62 eval { require MRO::Compat };
66 $impl = \&mro::get_linear_isa;
68 # VVVVV CODE TAKEN FROM MRO::COMPAT VVVVV
69 my $_get_linear_isa_dfs; # this recurses so it isn't pretty
70 $_get_linear_isa_dfs = sub {
73 my $classname = shift;
75 my @lin = ($classname);
77 foreach my $parent (@{"$classname\::ISA"}) {
78 my $plin = $_get_linear_isa_dfs->($parent);
79 foreach my $p(@$plin) {
80 next if exists $stored{$p};
87 # ^^^^^ CODE TAKEN FROM MRO::COMPAT ^^^^^
88 $impl = $_get_linear_isa_dfs;
94 *get_linear_isa = $impl;
97 { # taken from Sub::Identify
98 sub get_code_info($) {
100 ref($coderef) or return;
104 my $cv = B::svref_2object($coderef);
105 $cv->isa('B::CV') or return;
108 $gv->isa('B::GV') or return;
110 return ($gv->STASH->NAME, $gv->NAME);
114 # taken from Mouse::Util (0.90)
118 sub resolve_metaclass_alias {
119 my ( $type, $metaclass_name, %options ) = @_;
121 my $cache_key = $type . q{ } . ( $options{trait} ? '-Trait' : '' );
123 return $cache{$cache_key}{$metaclass_name} ||= do{
125 my $possible_full_name = join '::',
126 'Mouse::Meta', $type, 'Custom', ($options{trait} ? 'Trait' : ()), $metaclass_name
129 my $loaded_class = load_first_existing_class(
134 $loaded_class->can('register_implementation')
135 ? $loaded_class->register_implementation
141 # taken from Class/MOP.pm
142 sub is_valid_class_name {
145 return 0 if ref($class);
146 return 0 unless defined($class);
148 return 1 if $class =~ /^\w+(?:::\w+)*$/;
153 # taken from Class/MOP.pm
154 sub load_first_existing_class {
159 for my $class (@classes) {
160 my $e = _try_load_one_class($class);
163 $exceptions{$class} = $e;
174 sprintf( "Could not load class (%s) because : %s",
175 $_, $exceptions{$_} )
180 # taken from Class/MOP.pm
181 sub _try_load_one_class {
184 unless ( is_valid_class_name($class) ) {
185 my $display = defined($class) ? $class : 'undef';
186 confess "Invalid class name ($display)";
189 return if is_class_loaded($class);
191 my $file = $class . '.pm';
196 eval { require($file) };
204 my $e = _try_load_one_class($class);
205 confess "Could not load class ($class) because : $e" if $e;
210 my %is_class_loaded_cache;
211 sub is_class_loaded {
214 return 0 if ref($class) || !defined($class) || !length($class);
216 return 1 if $is_class_loaded_cache{$class};
218 # walk the symbol table tree to avoid autovififying
219 # \*{${main::}{"Foo::"}} == \*main::Foo::
222 foreach my $part (split('::', $class)) {
223 my $entry = \$pack->{$part . '::'};
224 return 0 if ref($entry) ne 'GLOB';
225 $pack = *{$entry}{HASH} or return 0;
228 # check for $VERSION or @ISA
229 return ++$is_class_loaded_cache{$class} if exists $pack->{VERSION}
230 && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} };
231 return ++$is_class_loaded_cache{$class} if exists $pack->{ISA}
232 && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0;
234 # check for any method
235 foreach my $name( keys %{$pack} ) {
236 my $entry = \$pack->{$name};
237 return ++$is_class_loaded_cache{$class} if ref($entry) ne 'GLOB' || defined *{$entry}{CODE};
245 sub apply_all_roles {
246 my $meta = Mouse::Meta::Class->initialize(shift);
250 # Basis of Data::OptList
251 my $max = scalar(@_);
252 for (my $i = 0; $i < $max ; $i++) {
253 if ($i + 1 < $max && ref($_[$i + 1])) {
254 push @roles, [ $_[$i++] => $_[$i] ];
256 push @roles, [ $_[$i] => undef ];
258 my $role_name = $roles[-1][0];
259 load_class($role_name);
260 ( $role_name->can('meta') && $role_name->meta->isa('Mouse::Meta::Role') )
261 || $meta->throw_error("You can only consume roles, $role_name(".$role_name->meta.") is not a Mouse role");
264 if ( scalar @roles == 1 ) {
265 my ( $role, $params ) = @{ $roles[0] };
266 $role->meta->apply( $meta, ( defined $params ? %$params : () ) );
269 Mouse::Meta::Role->combine_apply($meta, @roles);
274 # taken from Moose::Util 0.90
276 return $_[0] if @_ == 1;
280 return "$items[0] and $items[1]" if @items == 2;
282 my $tail = pop @items;
284 return join q{, }, @items, "and $tail";
293 $feature ||= ( caller(1) )[3]; # subroutine name
295 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
296 Carp::confess("Mouse does not currently support $feature");
300 return Mouse::Meta::Class->initialize($_[0]);
304 my($self, $maxdepth) = @_;
306 require 'Data/Dumper.pm'; # we don't want to create its namespace
307 my $dd = Data::Dumper->new([$self]);
308 $dd->Maxdepth(defined($maxdepth) ? $maxdepth : 2);
314 *does = \&does_role; # alias
322 Mouse::Util - features, with or without their dependencies
324 =head1 IMPLEMENTATIONS FOR
332 =head3 C<resolve_metaclass_alias>
334 =head3 C<apply_all_roles>
336 =head3 C<english_list>
340 =head2 C<< is_class_loaded(ClassName) -> Bool >>
342 Returns whether C<ClassName> is actually loaded or not. It uses a heuristic which
343 involves checking for the existence of C<$VERSION>, C<@ISA>, and any
344 locally-defined method.
346 =head3 C<< load_class(ClassName) >>
348 This will load a given C<ClassName> (or die if it's not loadable).
349 This function can be used in place of tricks like
350 C<eval "use $module"> or using C<require>.
354 =head3 C<get_linear_isa>
358 =head3 C<get_code_info>
360 =head1 UTILITIES FOR MOUSE