X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=lib%2FMouse%2FUtil.pm;h=f24da52f53263a3a64f8fe3d00d7cb5d7c1b322a;hp=c31e1150c22abc5a0551d340f8c8655020dd86c6;hb=df6dd016657118a06b408d21767dbc9b4ca476b9;hpb=bc69ee88207ce5c53f5c02dbd44cfabfbe6bae70 diff --git a/lib/Mouse/Util.pm b/lib/Mouse/Util.pm index c31e115..f24da52 100644 --- a/lib/Mouse/Util.pm +++ b/lib/Mouse/Util.pm @@ -1,8 +1,31 @@ package Mouse::Util; use Mouse::Exporter; # enables strict and warnings +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'; + + my $need_pp = !!$ENV{MOUSE_PUREPERL}; + + if(!$need_pp && !exists $INC{'Mouse/PurePerl.pm'}){ + local $@; + $need_pp = !eval{ + require XSLoader; + XSLoader::load('Mouse', $VERSION); + }; + warn $@ if $@; # for DEBUGGING + } + + if($need_pp){ + require 'Mouse/PurePerl.pm'; # we don't want to create its namespace + } +} + + use Carp qw(confess); -use B (); +use Scalar::Util qw(blessed); use constant _MOUSE_VERBOSE => !!$ENV{MOUSE_VERBOSE}; @@ -29,9 +52,10 @@ Mouse::Exporter->setup_import_methods( )], groups => { default => [], # export no functions by default + + # The ':meta' group is 'use metaclass' for Mouse meta => [qw(does meta dump _MOUSE_VERBOSE)], }, - _export_to_main => 1, ); # aliases as public APIs @@ -105,32 +129,6 @@ BEGIN { *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); - } - - 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) { @@ -161,6 +159,8 @@ BEGIN { # Utilities from Class::MOP +sub get_code_info; +sub get_code_package; # taken from Class/MOP.pm sub is_valid_class_name { @@ -232,41 +232,11 @@ sub load_class { 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 $meta = Mouse::Meta::Class->initialize(shift); + my $applicant = blessed($_[0]) ? shift : Mouse::Meta::Class->initialize(shift); my @roles; @@ -274,22 +244,24 @@ sub apply_all_roles { my $max = scalar(@_); for (my $i = 0; $i < $max ; $i++) { if ($i + 1 < $max && ref($_[$i + 1])) { - push @roles, [ $_[$i++] => $_[$i] ]; + push @roles, [ $_[$i] => $_[++$i] ]; } else { - push @roles, [ $_[$i] => undef ]; + push @roles, [ $_[$i] => undef ]; } my $role_name = $roles[-1][0]; load_class($role_name); - ( $role_name->can('meta') && $role_name->meta->isa('Mouse::Meta::Role') ) - || $meta->throw_error("You can only consume roles, $role_name(".$role_name->meta.") is not a Mouse role"); + + 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"); } if ( scalar @roles == 1 ) { - my ( $role, $params ) = @{ $roles[0] }; - $role->meta->apply( $meta, ( defined $params ? %$params : () ) ); + my ( $role_name, $params ) = @{ $roles[0] }; + get_metaclass_by_name($role_name)->apply( $applicant, defined $params ? $params : () ); } else { - Mouse::Meta::Role->combine_apply($meta, @roles); + Mouse::Meta::Role->combine(@roles)->apply($applicant); } return; } @@ -319,11 +291,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 @@ -333,6 +307,7 @@ sub dump { return $dd->Dump(); } +# general does() method sub does :method; *does = \&does_role; # alias @@ -344,6 +319,10 @@ __END__ Mouse::Util - Features, with or without their dependencies +=head1 VERSION + +This document describes Mouse version 0.40 + =head1 IMPLEMENTATIONS FOR =head2 Moose::Util