X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=lib%2FMouse%2FUtil.pm;h=1bf2f7d2567304cdc728da1eddd369f26b683ad8;hp=a34870026c8bf4dfd7d71b56bf90d7439e062dde;hb=deb9a0f32002cd07012c50884a227335b93f1449;hpb=f3bb863f6a6ef09220bbf51bc4cea3874d862776 diff --git a/lib/Mouse/Util.pm b/lib/Mouse/Util.pm index a348700..1bf2f7d 100644 --- a/lib/Mouse/Util.pm +++ b/lib/Mouse/Util.pm @@ -5,6 +5,9 @@ use warnings; use Exporter; use Carp qw(confess); +use B (); + +use constant _MOUSE_VERBOSE => !!$ENV{MOUSE_VERBOSE}; our @ISA = qw(Exporter); our @EXPORT_OK = qw( @@ -20,25 +23,40 @@ our @EXPORT_OK = qw( get_linear_isa get_code_info + get_code_package + not_supported does meta dump + _MOUSE_VERBOSE ); our %EXPORT_TAGS = ( all => \@EXPORT_OK, - meta => [qw(does meta dump)], + meta => [qw(does meta dump _MOUSE_VERBOSE)], ); +# aliases as public APIs + +# it must be 'require', not 'use', because Mouse::Meta::Module depends on Mouse::Util +require Mouse::Meta::Module; # for the entities of metaclass cache utilities + +BEGIN { + *class_of = \&Mouse::Meta::Module::class_of; + *get_metaclass_by_name = \&Mouse::Meta::Module::get_metaclass_by_name; + *get_all_metaclass_instances = \&Mouse::Meta::Module::get_all_metaclass_instances; + *get_all_metaclass_names = \&Mouse::Meta::Module::get_all_metaclass_names; +} + # Moose::Util compatible utilities sub find_meta{ - return Mouse::Meta::Module::class_of( $_[0] ); + return class_of( $_[0] ); } sub does_role{ my ($class_or_obj, $role_name) = @_; - my $meta = Mouse::Meta::Module::class_of($class_or_obj); + my $meta = class_of($class_or_obj); (defined $role_name) || ($meta || 'Mouse::Meta::Class')->throw_error("You must supply a role name to does()"); @@ -46,8 +64,6 @@ sub does_role{ return defined($meta) && $meta->does_role($role_name); } - - BEGIN { my $impl; if ($] >= 5.009_005) { @@ -96,8 +112,6 @@ BEGIN { my ($coderef) = @_; ref($coderef) or return; - require B; - my $cv = B::svref_2object($coderef); $cv->isa('B::CV') or return; @@ -106,6 +120,18 @@ BEGIN { return ($gv->STASH->NAME, $gv->NAME); } + + sub get_code_package{ + my($coderef) = @_; + + my $cv = B::svref_2object($coderef); + $cv->isa('B::CV') or return ''; + + my $gv = $cv->GV; + $gv->isa('B::GV') or return ''; + + return $gv->STASH->NAME; + } } # taken from Mouse::Util (0.90) @@ -135,6 +161,9 @@ BEGIN { } } +# Utilities from Class::MOP + + # taken from Class/MOP.pm sub is_valid_class_name { my $class = shift; @@ -175,6 +204,7 @@ sub load_first_existing_class { } # taken from Class/MOP.pm +my %is_class_loaded_cache; sub _try_load_one_class { my $class = shift; @@ -183,7 +213,7 @@ sub _try_load_one_class { confess "Invalid class name ($display)"; } - return if is_class_loaded($class); + return undef if $is_class_loaded_cache{$class} ||= is_class_loaded($class); my $file = $class . '.pm'; $file =~ s{::}{/}g; @@ -204,14 +234,12 @@ sub load_class { return 1; } -my %is_class_loaded_cache; + sub is_class_loaded { my $class = shift; return 0 if ref($class) || !defined($class) || !length($class); - return 1 if $is_class_loaded_cache{$class}; - # walk the symbol table tree to avoid autovififying # \*{${main::}{"Foo::"}} == \*main::Foo:: @@ -223,15 +251,15 @@ sub is_class_loaded { } # check for $VERSION or @ISA - return ++$is_class_loaded_cache{$class} if exists $pack->{VERSION} + return 1 if exists $pack->{VERSION} && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} }; - return ++$is_class_loaded_cache{$class} if exists $pack->{ISA} + return 1 if exists $pack->{ISA} && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0; # check for any method foreach my $name( keys %{$pack} ) { my $entry = \$pack->{$name}; - return ++$is_class_loaded_cache{$class} if ref($entry) ne 'GLOB' || defined *{$entry}{CODE}; + return 1 if ref($entry) ne 'GLOB' || defined *{$entry}{CODE}; } # fail @@ -316,7 +344,7 @@ __END__ =head1 NAME -Mouse::Util - features, with or without their dependencies +Mouse::Util - Features, with or without their dependencies =head1 IMPLEMENTATIONS FOR @@ -334,7 +362,7 @@ Mouse::Util - features, with or without their dependencies =head2 Class::MOP -=head2 C<< is_class_loaded(ClassName) -> Bool >> +=head3 C<< is_class_loaded(ClassName) -> Bool >> Returns whether C is actually loaded or not. It uses a heuristic which involves checking for the existence of C<$VERSION>, C<@ISA>, and any @@ -342,10 +370,18 @@ locally-defined method. =head3 C<< load_class(ClassName) >> -This will load a given C (or die if it's not loadable). +This will load a given C (or die if it is not loadable). This function can be used in place of tricks like C or using C. +=head2 C<< Mouse::Util::class_of(ClassName) >> + +The counterpart of C. This is not exportable. + +=head2 C<< Mouse::Util::get_metaclass_by_name(ClassName) >> + +The counterpart of C. This is not exportable. + =head2 MRO::Compat =head3 C @@ -356,13 +392,7 @@ C or using C. =head1 UTILITIES FOR MOUSE -=over 4 - -=item * - -C - -=back +=head3 C =head1 SEE ALSO