X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=lib%2FMouse%2FUtil.pm;h=3ffa37550e5b66e2183a239938973080b5b5e00b;hp=9d5346c0bdc83e9267a243928192a360f422fdfd;hb=5af36247683101e3c457450489486d41f0bd7101;hpb=b1980b8685d838da4ca48e82596e2fb3ecb5341b diff --git a/lib/Mouse/Util.pm b/lib/Mouse/Util.pm index 9d5346c..3ffa375 100644 --- a/lib/Mouse/Util.pm +++ b/lib/Mouse/Util.pm @@ -1,51 +1,99 @@ package Mouse::Util; use Mouse::Exporter; # enables strict and warnings -use Carp qw(confess); -use Scalar::Util qw(blessed); -use B (); +sub get_linear_isa($;$); # must be here -use constant _MOUSE_VERBOSE => !!$ENV{MOUSE_VERBOSE}; +BEGIN{ + # This is used in Mouse::PurePerl + Mouse::Exporter->setup_import_methods( + as_is => [qw( + find_meta + does_role + resolve_metaclass_alias + apply_all_roles + english_list + + load_class + is_class_loaded + + get_linear_isa + get_code_info + + get_code_package + get_code_ref + + not_supported + + does meta dump + )], + groups => { + default => [], # export no functions by default + + # The ':meta' group is 'use metaclass' for Mouse + meta => [qw(does meta dump)], + }, + ); -Mouse::Exporter->setup_import_methods( - as_is => [qw( - find_meta - does_role - resolve_metaclass_alias - apply_all_roles - english_list - load_class - is_class_loaded + # Because Mouse::Util is loaded first in all the Mouse sub-modules, + # XS loader is placed here, not in Mouse.pm. - get_linear_isa - get_code_info + our $VERSION = '0.49'; - get_code_package + my $xs = !(exists $INC{'Mouse/PurePerl.pm'} || $ENV{MOUSE_PUREPERL}); + + if($xs){ + # XXX: XSLoader tries to get the object path from caller's file name + # $hack_mouse_file fools its mechanism + + (my $hack_mouse_file = __FILE__) =~ s/.Util//; # .../Mouse/Util.pm -> .../Mouse.pm + $xs = eval sprintf("#line %d %s\n", __LINE__, $hack_mouse_file) . q{ + require XSLoader; + XSLoader::load('Mouse', $VERSION); + Mouse::Util->import({ into => 'Mouse::Meta::Method::Constructor::XS' }, ':meta'); + Mouse::Util->import({ into => 'Mouse::Meta::Method::Destructor::XS' }, ':meta'); + Mouse::Util->import({ into => 'Mouse::Meta::Method::Accessor::XS' }, ':meta'); + return 1; + } || 0; + #warn $@ if $@; + } + + if(!$xs){ + require 'Mouse/PurePerl.pm'; # we don't want to create its namespace + } + + *MOUSE_XS = sub(){ $xs }; +} - not_supported +use Carp (); +use Scalar::Util (); - does meta dump - _MOUSE_VERBOSE - )], - groups => { - default => [], # export no functions by default - meta => [qw(does meta dump _MOUSE_VERBOSE)], - }, - _export_to_main => 1, -); +use constant _MOUSE_VERBOSE => !!$ENV{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; + *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; + + # is-a predicates + #generate_isa_predicate_for('Mouse::Meta::TypeConstraint' => 'is_a_type_constraint'); + #generate_isa_predicate_for('Mouse::Meta::Class' => 'is_a_metaclass'); + #generate_isa_predicate_for('Mouse::Meta::Role' => 'is_a_metarole'); + + # duck type predicates + generate_can_predicate_for(['_compiled_type_constraint'] => 'is_a_type_constraint'); + generate_can_predicate_for(['create_anon_class'] => 'is_a_metaclass'); + generate_can_predicate_for(['create_anon_role'] => 'is_a_metarole'); } +our $in_global_destruction = 0; +END{ $in_global_destruction = 1 } + # Moose::Util compatible utilities sub find_meta{ @@ -64,75 +112,55 @@ sub does_role{ } BEGIN { - my $impl; + my $get_linear_isa; if ($] >= 5.009_005) { require mro; - $impl = \&mro::get_linear_isa; + $get_linear_isa = \&mro::get_linear_isa; } else { - my $e = do { - local $@; - eval { require MRO::Compat }; - $@; - }; - if (!$e) { - $impl = \&mro::get_linear_isa; - } else { # VVVVV CODE TAKEN FROM MRO::COMPAT VVVVV - my $_get_linear_isa_dfs; # this recurses so it isn't pretty - $_get_linear_isa_dfs = sub { - no strict 'refs'; - - my $classname = shift; - - my @lin = ($classname); - my %stored; - foreach my $parent (@{"$classname\::ISA"}) { - my $plin = $_get_linear_isa_dfs->($parent); - foreach my $p(@$plin) { - next if exists $stored{$p}; - push(@lin, $p); - $stored{$p} = 1; - } + my $_get_linear_isa_dfs; # this recurses so it isn't pretty + $_get_linear_isa_dfs = sub { + my($classname) = @_; + + my @lin = ($classname); + my %stored; + + no strict 'refs'; + foreach my $parent (@{"$classname\::ISA"}) { + my $plin = $_get_linear_isa_dfs->($parent); + foreach my $p(@$plin) { + next if exists $stored{$p}; + push(@lin, $p); + $stored{$p} = 1; } - return \@lin; - }; + } + return \@lin; + }; # ^^^^^ CODE TAKEN FROM MRO::COMPAT ^^^^^ - $impl = $_get_linear_isa_dfs; - } - } - - no warnings 'once'; - *get_linear_isa = $impl; -} - -{ # taken from Sub::Identify - sub get_code_info($) { - my ($coderef) = @_; - ref($coderef) or return; - - 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, $gv->NAME); + eval{ require Class::C3 }; + + # MRO::Compat::__get_linear_isa has no prototype, so + # we define a prototyped version for compatibility with core's + # See also MRO::Compat::__get_linear_isa. + $get_linear_isa = sub ($;$){ + my($classname, $type) = @_; + package # hide from PAUSE + Class::C3; + if(!defined $type){ + our %MRO; + $type = exists $MRO{$classname} ? 'c3' : 'dfs'; + } + return $type eq 'c3' + ? [calculateMRO($classname)] + : $_get_linear_isa_dfs->($classname); + }; } - 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; - } + *get_linear_isa = $get_linear_isa; } + # taken from Mouse::Util (0.90) { my %cache; @@ -162,6 +190,8 @@ BEGIN { # Utilities from Class::MOP +sub get_code_info; +sub get_code_package; # taken from Class/MOP.pm sub is_valid_class_name { @@ -170,7 +200,7 @@ sub is_valid_class_name { return 0 if ref($class); return 0 unless defined($class); - return 1 if $class =~ /^\w+(?:::\w+)*$/; + return 1 if $class =~ /\A \w+ (?: :: \w+ )* \z/xms; return 0; } @@ -193,7 +223,7 @@ sub load_first_existing_class { } # not found - confess join( + Carp::confess join( "\n", map { sprintf( "Could not load class (%s) because : %s", @@ -209,7 +239,7 @@ sub _try_load_one_class { unless ( is_valid_class_name($class) ) { my $display = defined($class) ? $class : 'undef'; - confess "Invalid class name ($display)"; + Carp::confess "Invalid class name ($display)"; } return undef if $is_class_loaded_cache{$class} ||= is_class_loaded($class); @@ -228,46 +258,17 @@ sub _try_load_one_class { sub load_class { my $class = shift; my $e = _try_load_one_class($class); - confess "Could not load class ($class) because : $e" if $e; + Carp::confess "Could not load class ($class) because : $e" if $e; return 1; } - -sub is_class_loaded { - my $class = shift; - - return 0 if ref($class) || !defined($class) || !length($class); - - # walk the symbol table tree to avoid autovififying - # \*{${main::}{"Foo::"}} == \*main::Foo:: - - my $pack = \%::; - foreach my $part (split('::', $class)) { - my $entry = \$pack->{$part . '::'}; - return 0 if ref($entry) ne 'GLOB'; - $pack = *{$entry}{HASH} or return 0; - } - - # check for $VERSION or @ISA - return 1 if exists $pack->{VERSION} - && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} }; - 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 1 if ref($entry) ne 'GLOB' || defined *{$entry}{CODE}; - } - - # fail - return 0; -} - +sub is_class_loaded; sub apply_all_roles { - my $applicant = blessed($_[0]) ? shift : Mouse::Meta::Class->initialize(shift); + my $consumer = Scalar::Util::blessed($_[0]) + ? shift # instance + : Mouse::Meta::Class->initialize(shift); # class or role name my @roles; @@ -281,17 +282,17 @@ sub apply_all_roles { } my $role_name = $roles[-1][0]; load_class($role_name); - my $metarole = get_metaclass_by_name($role_name); - ( $metarole && $metarole->isa('Mouse::Meta::Role') ) - || $applicant->meta->throw_error("You can only consume roles, $role_name(".$role_name->meta.") is not a Mouse role"); + + is_a_metarole( get_metaclass_by_name($role_name) ) + || $consumer->meta->throw_error("You can only consume roles, $role_name is not a Mouse role"); } if ( scalar @roles == 1 ) { my ( $role_name, $params ) = @{ $roles[0] }; - get_metaclass_by_name($role_name)->apply( $applicant, defined $params ? $params : () ); + get_metaclass_by_name($role_name)->apply( $consumer, defined $params ? $params : () ); } else { - Mouse::Meta::Role->combine(@roles)->apply($applicant); + Mouse::Meta::Role->combine(@roles)->apply($consumer); } return; } @@ -309,6 +310,17 @@ sub english_list { return join q{, }, @items, "and $tail"; } +sub quoted_english_list { + return qq{'$_[0]'} if @_ == 1; + + my @items = sort @_; + + return qq{'$items[0]' and '$items[1]'} if @items == 2; + + my $tail = pop @items; + + return join q{, }, (map{ qq{'$_'} } @items), qq{and '$tail'}; +} # common utilities @@ -321,11 +333,13 @@ sub not_supported{ Carp::confess("Mouse does not currently support $feature"); } -sub meta{ +# general meta() method +sub meta :method{ return Mouse::Meta::Class->initialize(ref($_[0]) || $_[0]); } -sub dump { +# general dump() method +sub dump :method { my($self, $maxdepth) = @_; require 'Data/Dumper.pm'; # we don't want to create its namespace @@ -335,17 +349,21 @@ sub dump { return $dd->Dump(); } +# general does() method sub does :method; *does = \&does_role; # alias 1; - __END__ =head1 NAME Mouse::Util - Features, with or without their dependencies +=head1 VERSION + +This document describes Mouse version 0.49 + =head1 IMPLEMENTATIONS FOR =head2 Moose::Util @@ -390,10 +408,14 @@ C or using C. =head3 C -=head1 UTILITIES FOR MOUSE +=head1 Mouse specific utilities =head3 C +=head3 C + +=head3 C + =head1 SEE ALSO L