Refactoring
[gitmo/Mouse.git] / lib / Mouse / Util.pm
index 1bb4d41..ff39422 100644 (file)
@@ -2,11 +2,16 @@ package Mouse::Util;
 use strict;
 use warnings;
 use base qw/Exporter/;
-use Carp;
+use Carp qw(confess);
+use B ();
 
 our @EXPORT_OK = qw(
     get_linear_isa
     apply_all_roles
+    version 
+    authority
+    identifier
+    get_code_info
 );
 our %EXPORT_TAGS = (
     all  => \@EXPORT_OK,
@@ -18,16 +23,17 @@ BEGIN {
         require mro;
         $impl = \&mro::get_linear_isa;
     } else {
-        my $loaded = do {
-            local $SIG{__DIE__} = 'DEFAULT';
-            eval "require MRO::Compat; 1";
+        my $e = do {
+            local $@;
+            eval { require MRO::Compat };
+            $@;
         };
-        if ($loaded) {
+        if (!$e) {
             $impl = \&mro::get_linear_isa;
         } else {
 #       VVVVV   CODE TAKEN FROM MRO::COMPAT   VVVVV
-            my $code; # this recurses so it isn't pretty
-            $code = sub {
+            my $_get_linear_isa_dfs; # this recurses so it isn't pretty
+            $_get_linear_isa_dfs = sub {
                 no strict 'refs';
 
                 my $classname = shift;
@@ -35,17 +41,17 @@ BEGIN {
                 my @lin = ($classname);
                 my %stored;
                 foreach my $parent (@{"$classname\::ISA"}) {
-                    my $plin = $code->($parent);
-                    foreach (@$plin) {
-                        next if exists $stored{$_};
-                        push(@lin, $_);
-                        $stored{$_} = 1;
+                    my $plin = $_get_linear_isa_dfs->($parent);
+                    foreach  my $p(@$plin) {
+                        next if exists $stored{$p};
+                        push(@lin, $p);
+                        $stored{$p} = 1;
                     }
                 }
                 return \@lin;
             };
 #       ^^^^^   CODE TAKEN FROM MRO::COMPAT   ^^^^^
-            $impl = $code;
+            $impl = $_get_linear_isa_dfs;
         }
     }
 
@@ -53,6 +59,35 @@ BEGIN {
     *{ __PACKAGE__ . '::get_linear_isa'} = $impl;
 }
 
+{ # taken from Sub::Identify
+    sub get_code_info($) {\r
+        my ($coderef) = @_;\r
+        ref($coderef) or return;\r
+        my $cv = B::svref_2object($coderef);\r
+        $cv->isa('B::CV') or return;
+
+        my $gv = $cv->GV;\r
+        # bail out if GV is undefined\r
+        $gv->isa('B::SPECIAL') and return;\r
+\r
+        return ($gv->STASH->NAME, $gv->NAME);\r
+    }\r
+}
+
+{ # adapted from Class::MOP::Module
+
+    sub version { no strict 'refs'; ${shift->name.'::VERSION'} }
+    sub authority { no strict 'refs'; ${shift->name.'::AUTHORITY'} }  
+    sub identifier {
+        my $self = shift;
+        join '-' => (
+            $self->name,
+            ($self->version   || ()),
+            ($self->authority || ()),
+        );
+    }
+}
+
 # taken from Class/MOP.pm
 {
     my %cache;
@@ -166,7 +201,7 @@ sub apply_all_roles {
     }
 
     ( $_->[0]->can('meta') && $_->[0]->meta->isa('Mouse::Meta::Role') )
-        || croak("You can only consume roles, "
+        || confess("You can only consume roles, "
         . $_->[0]
         . " is not a Moose role")
         foreach @roles;