X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=lib%2FMouse%2FUtil.pm;h=a34870026c8bf4dfd7d71b56bf90d7439e062dde;hp=35c117d1acbf2c6163acfe6ae41cd8e7433ceff9;hb=f3bb863f6a6ef09220bbf51bc4cea3874d862776;hpb=ea249879d28f5ce42afb95feeb7dcb6d37fcd241 diff --git a/lib/Mouse/Util.pm b/lib/Mouse/Util.pm index 35c117d..a348700 100644 --- a/lib/Mouse/Util.pm +++ b/lib/Mouse/Util.pm @@ -1,11 +1,12 @@ package Mouse::Util; use strict; use warnings; -use base qw/Exporter/; + +use Exporter; use Carp qw(confess); -use B (); +our @ISA = qw(Exporter); our @EXPORT_OK = qw( find_meta does_role @@ -20,9 +21,12 @@ our @EXPORT_OK = qw( get_code_info not_supported + + does meta dump ); our %EXPORT_TAGS = ( all => \@EXPORT_OK, + meta => [qw(does meta dump)], ); # Moose::Util compatible utilities @@ -32,13 +36,14 @@ sub find_meta{ } sub does_role{ - my ($class_or_obj, $role) = @_; + my ($class_or_obj, $role_name) = @_; my $meta = Mouse::Meta::Module::class_of($class_or_obj); - return 0 unless defined $meta; - return 1 if $meta->does_role($role); - return 0; + (defined $role_name) + || ($meta || 'Mouse::Meta::Class')->throw_error("You must supply a role name to does()"); + + return defined($meta) && $meta->does_role($role_name); } @@ -91,6 +96,8 @@ BEGIN { my ($coderef) = @_; ref($coderef) or return; + require B; + my $cv = B::svref_2object($coderef); $cv->isa('B::CV') or return; @@ -145,7 +152,6 @@ sub load_first_existing_class { my @classes = @_ or return; - my $found; my %exceptions; for my $class (@classes) { my $e = _try_load_one_class($class); @@ -154,12 +160,11 @@ sub load_first_existing_class { $exceptions{$class} = $e; } else { - $found = $class; - last; + return $class; } } - return $found if $found; + # not found confess join( "\n", map { @@ -245,7 +250,7 @@ sub apply_all_roles { if ($i + 1 < $max && ref($_[$i + 1])) { push @roles, [ $_[$i++] => $_[$i] ]; } else { - push @roles, [ $_[$i] => {} ]; + push @roles, [ $_[$i] => undef ]; } my $role_name = $roles[-1][0]; load_class($role_name); @@ -276,6 +281,9 @@ sub english_list { return join q{, }, @items, "and $tail"; } + +# common utilities + sub not_supported{ my($feature) = @_; @@ -285,6 +293,23 @@ sub not_supported{ Carp::confess("Mouse does not currently support $feature"); } +sub meta{ + return Mouse::Meta::Class->initialize($_[0]); +} + +sub dump { + my($self, $maxdepth) = @_; + + require 'Data/Dumper.pm'; # we don't want to create its namespace + my $dd = Data::Dumper->new([$self]); + $dd->Maxdepth(defined($maxdepth) ? $maxdepth : 2); + $dd->Indent(1); + return $dd->Dump(); +} + +sub does :method; +*does = \&does_role; # alias + 1; __END__ @@ -309,9 +334,17 @@ Mouse::Util - features, with or without their dependencies =head2 Class::MOP -=head3 C +=head2 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 +locally-defined method. + +=head3 C<< load_class(ClassName) >> -=head3 C +This will load a given C (or die if it's not loadable). +This function can be used in place of tricks like +C or using C. =head2 MRO::Compat @@ -331,5 +364,15 @@ C =back +=head1 SEE ALSO + +L + +L + +L + +L + =cut