9 our @ISA = qw(Exporter);
13 resolve_metaclass_alias
29 meta => [qw(does meta dump)],
32 # Moose::Util compatible utilities
35 return Mouse::Meta::Module::class_of( $_[0] );
39 my ($class_or_obj, $role_name) = @_;
41 my $meta = Mouse::Meta::Module::class_of($class_or_obj);
44 || ($meta || 'Mouse::Meta::Class')->throw_error("You must supply a role name to does()");
46 return defined($meta) && $meta->does_role($role_name);
53 if ($] >= 5.009_005) {
55 $impl = \&mro::get_linear_isa;
59 eval { require MRO::Compat };
63 $impl = \&mro::get_linear_isa;
65 # VVVVV CODE TAKEN FROM MRO::COMPAT VVVVV
66 my $_get_linear_isa_dfs; # this recurses so it isn't pretty
67 $_get_linear_isa_dfs = sub {
70 my $classname = shift;
72 my @lin = ($classname);
74 foreach my $parent (@{"$classname\::ISA"}) {
75 my $plin = $_get_linear_isa_dfs->($parent);
76 foreach my $p(@$plin) {
77 next if exists $stored{$p};
84 # ^^^^^ CODE TAKEN FROM MRO::COMPAT ^^^^^
85 $impl = $_get_linear_isa_dfs;
91 *get_linear_isa = $impl;
94 { # taken from Sub::Identify
95 sub get_code_info($) {
97 ref($coderef) or return;
101 my $cv = B::svref_2object($coderef);
102 $cv->isa('B::CV') or return;
105 $gv->isa('B::GV') or return;
107 return ($gv->STASH->NAME, $gv->NAME);
111 # taken from Mouse::Util (0.90)
115 sub resolve_metaclass_alias {
116 my ( $type, $metaclass_name, %options ) = @_;
118 my $cache_key = $type . q{ } . ( $options{trait} ? '-Trait' : '' );
120 return $cache{$cache_key}{$metaclass_name} ||= do{
122 my $possible_full_name = join '::',
123 'Mouse::Meta', $type, 'Custom', ($options{trait} ? 'Trait' : ()), $metaclass_name
126 my $loaded_class = load_first_existing_class(
131 $loaded_class->can('register_implementation')
132 ? $loaded_class->register_implementation
138 # taken from Class/MOP.pm
139 sub is_valid_class_name {
142 return 0 if ref($class);
143 return 0 unless defined($class);
145 return 1 if $class =~ /^\w+(?:::\w+)*$/;
150 # taken from Class/MOP.pm
151 sub load_first_existing_class {
156 for my $class (@classes) {
157 my $e = _try_load_one_class($class);
160 $exceptions{$class} = $e;
171 sprintf( "Could not load class (%s) because : %s",
172 $_, $exceptions{$_} )
177 # taken from Class/MOP.pm
178 sub _try_load_one_class {
181 unless ( is_valid_class_name($class) ) {
182 my $display = defined($class) ? $class : 'undef';
183 confess "Invalid class name ($display)";
186 return if is_class_loaded($class);
188 my $file = $class . '.pm';
193 eval { require($file) };
201 my $e = _try_load_one_class($class);
202 confess "Could not load class ($class) because : $e" if $e;
207 my %is_class_loaded_cache;
208 sub is_class_loaded {
211 return 0 if ref($class) || !defined($class) || !length($class);
213 return 1 if $is_class_loaded_cache{$class};
215 # walk the symbol table tree to avoid autovififying
216 # \*{${main::}{"Foo::"}} == \*main::Foo::
219 foreach my $part (split('::', $class)) {
220 my $entry = \$pack->{$part . '::'};
221 return 0 if ref($entry) ne 'GLOB';
222 $pack = *{$entry}{HASH} or return 0;
225 # check for $VERSION or @ISA
226 return ++$is_class_loaded_cache{$class} if exists $pack->{VERSION}
227 && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} };
228 return ++$is_class_loaded_cache{$class} if exists $pack->{ISA}
229 && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0;
231 # check for any method
232 foreach my $name( keys %{$pack} ) {
233 my $entry = \$pack->{$name};
234 return ++$is_class_loaded_cache{$class} if ref($entry) ne 'GLOB' || defined *{$entry}{CODE};
242 sub apply_all_roles {
243 my $meta = Mouse::Meta::Class->initialize(shift);
247 # Basis of Data::OptList
248 my $max = scalar(@_);
249 for (my $i = 0; $i < $max ; $i++) {
250 if ($i + 1 < $max && ref($_[$i + 1])) {
251 push @roles, [ $_[$i++] => $_[$i] ];
253 push @roles, [ $_[$i] => undef ];
255 my $role_name = $roles[-1][0];
256 load_class($role_name);
257 ( $role_name->can('meta') && $role_name->meta->isa('Mouse::Meta::Role') )
258 || $meta->throw_error("You can only consume roles, $role_name(".$role_name->meta.") is not a Mouse role");
261 if ( scalar @roles == 1 ) {
262 my ( $role, $params ) = @{ $roles[0] };
263 $role->meta->apply( $meta, ( defined $params ? %$params : () ) );
266 Mouse::Meta::Role->combine_apply($meta, @roles);
271 # taken from Moose::Util 0.90
273 return $_[0] if @_ == 1;
277 return "$items[0] and $items[1]" if @items == 2;
279 my $tail = pop @items;
281 return join q{, }, @items, "and $tail";
290 $feature ||= ( caller(1) )[3]; # subroutine name
292 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
293 Carp::confess("Mouse does not currently support $feature");
297 return Mouse::Meta::Class->initialize($_[0]);
301 my($self, $maxdepth) = @_;
303 require 'Data/Dumper.pm'; # we don't want to create its namespace
304 my $dd = Data::Dumper->new([$self]);
305 $dd->Maxdepth(defined($maxdepth) ? $maxdepth : 2);
311 *does = \&does_role; # alias
319 Mouse::Util - features, with or without their dependencies
321 =head1 IMPLEMENTATIONS FOR
329 =head3 C<resolve_metaclass_alias>
331 =head3 C<apply_all_roles>
333 =head3 C<english_list>
337 =head2 C<< is_class_loaded(ClassName) -> Bool >>
339 Returns whether C<ClassName> is actually loaded or not. It uses a heuristic which
340 involves checking for the existence of C<$VERSION>, C<@ISA>, and any
341 locally-defined method.
343 =head3 C<< load_class(ClassName) >>
345 This will load a given C<ClassName> (or die if it's not loadable).
346 This function can be used in place of tricks like
347 C<eval "use $module"> or using C<require>.
351 =head3 C<get_linear_isa>
355 =head3 C<get_code_info>
357 =head1 UTILITIES FOR MOUSE