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=a226e1e30c44dd57e25f6ac220ef1803a0c73733;hb=5af36247683101e3c457450489486d41f0bd7101;hpb=8aba926dbf11e9cf418c7c79b925d15e60e1e990 diff --git a/lib/Mouse/Util.pm b/lib/Mouse/Util.pm index a226e1e..3ffa375 100644 --- a/lib/Mouse/Util.pm +++ b/lib/Mouse/Util.pm @@ -38,13 +38,11 @@ BEGIN{ # Because Mouse::Util is loaded first in all the Mouse sub-modules, # XS loader is placed here, not in Mouse.pm. - our $VERSION = '0.40_07'; + our $VERSION = '0.49'; my $xs = !(exists $INC{'Mouse/PurePerl.pm'} || $ENV{MOUSE_PUREPERL}); if($xs){ - local $@; - # XXX: XSLoader tries to get the object path from caller's file name # $hack_mouse_file fools its mechanism @@ -52,7 +50,11 @@ BEGIN{ $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 $@; } @@ -60,10 +62,9 @@ BEGIN{ require 'Mouse/PurePerl.pm'; # we don't want to create its namespace } - *_MOUSE_XS = sub(){ $xs }; + *MOUSE_XS = sub(){ $xs }; } - use Carp (); use Scalar::Util (); @@ -80,11 +81,18 @@ BEGIN { *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'); + #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 @@ -109,36 +117,44 @@ BEGIN { require mro; $get_linear_isa = \&mro::get_linear_isa; } else { - my $e = do { - local $@; - eval { require MRO::Compat }; - $@; - }; - if (!$e) { - $get_linear_isa = \&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 ^^^^^ - $get_linear_isa = $_get_linear_isa_dfs; - } + + 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); + }; } *get_linear_isa = $get_linear_isa; @@ -249,9 +265,8 @@ sub load_class { sub is_class_loaded; - sub apply_all_roles { - my $applicant = Scalar::Util::blessed($_[0]) + my $consumer = Scalar::Util::blessed($_[0]) ? shift # instance : Mouse::Meta::Class->initialize(shift); # class or role name @@ -269,15 +284,15 @@ sub apply_all_roles { load_class($role_name); is_a_metarole( get_metaclass_by_name($role_name) ) - || $applicant->meta->throw_error("You can only consume roles, $role_name(".$role_name->meta.") is not a Mouse role"); + || $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; } @@ -295,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 @@ -328,7 +354,6 @@ sub does :method; *does = \&does_role; # alias 1; - __END__ =head1 NAME @@ -337,7 +362,7 @@ Mouse::Util - Features, with or without their dependencies =head1 VERSION -This document describes Mouse version 0.40_07 +This document describes Mouse version 0.49 =head1 IMPLEMENTATIONS FOR @@ -383,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