The default maxdepth of 'dump' method is now 3, instead of 2.
[gitmo/Mouse.git] / lib / Mouse / Util.pm
index a226e1e..b7ab693 100644 (file)
@@ -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
 
@@ -318,17 +344,17 @@ sub dump :method {
 
     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->Maxdepth(defined($maxdepth) ? $maxdepth : 3);
     $dd->Indent(1);
     return $dd->Dump();
 }
 
 # general does() method
-sub does :method;
-*does = \&does_role; # alias
+sub does :method {
+    goto &does_role;
+}
 
 1;
-
 __END__
 
 =head1 NAME
@@ -337,7 +363,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 +409,14 @@ C<eval "use $module"> or using C<require>.
 
 =head3 C<get_code_info>
 
-=head1 UTILITIES FOR MOUSE
+=head1 Mouse specific utilities
 
 =head3 C<not_supported>
 
+=head3 C<get_code_package>
+
+=head3 C<get_code_ref>
+
 =head1 SEE ALSO
 
 L<Moose::Util>