fix for previous failing test (_concrete_methods_of returns a hashref, not a list...
[gitmo/Role-Tiny.git] / lib / Role / Tiny.pm
index 0cec861..9ce2caf 100644 (file)
@@ -6,7 +6,7 @@ sub _getstash { \%{"$_[0]::"} }
 use strict;
 use warnings FATAL => 'all';
 
-our $VERSION = '1.002001'; # 1.2.1
+our $VERSION = '1.002005'; # 1.2.5
 $VERSION = eval $VERSION;
 
 our %INFO;
@@ -45,7 +45,7 @@ sub import {
   warnings->import(FATAL => 'all');
   return if $INFO{$target}; # already exported into this package
   # get symbol table reference
-  my $stash = do { no strict 'refs'; \%{"${target}::"} };
+  my $stash = _getstash($target);
   # install before/after/around subs
   foreach my $type (qw(before after around)) {
     *{_getglob "${target}::${type}"} = sub {
@@ -64,9 +64,9 @@ sub import {
   };
   # grab all *non-constant* (stash slot is not a scalarref) subs present
   # in the symbol table and store their refaddrs (no need to forcibly
-  # inflate constant subs into real subs) - also add '' to here (this
-  # is used later) with a map to the coderefs in case of copying or re-use
-  my @not_methods = ('', map { *$_{CODE}||() } grep !ref($_), values %$stash);
+  # inflate constant subs into real subs) with a map to the coderefs in
+  # case of copying or re-use
+  my @not_methods = (map { *$_{CODE}||() } grep !ref($_), values %$stash);
   @{$INFO{$target}{not_methods}={}}{@not_methods} = @not_methods;
   # a role does itself
   $APPLIED_TO{$target} = { $target => undef };
@@ -171,7 +171,7 @@ sub apply_roles_to_package {
   return $me->apply_role_to_package($to, $roles[0]) if @roles == 1;
 
   my %conflicts = %{$me->_composite_info_for(@roles)->{conflicts}};
-  delete $conflicts{$_} for $me->_concrete_methods_of($to);
+  delete $conflicts{$_} for keys %{ $me->_concrete_methods_of($to) };
   if (keys %conflicts) {
     my $fail = 
       join "\n",
@@ -190,14 +190,18 @@ sub apply_roles_to_package {
     delete $INFO{$to}{methods}; # reset since we're about to add methods
   }
 
-  $me->apply_role_to_package($to, $_) for @roles;
+  foreach my $role (@roles) {
+    $me->apply_single_role_to_package($to, $role);
+  }
   $APPLIED_TO{$to}{join('|',@roles)} = 1;
 }
 
 sub _composite_info_for {
   my ($me, @roles) = @_;
   $COMPOSITE_INFO{join('|', sort @roles)} ||= do {
-    _load_module($_) for @roles;
+    foreach my $role (@roles) {
+      _load_module($role);
+    }
     my %methods;
     foreach my $role (@roles) {
       my $this_methods = $me->_concrete_methods_of($role);
@@ -214,7 +218,11 @@ sub _composable_package_for {
   return $composed_name if $COMPOSED{role}{$composed_name};
   $me->_install_methods($composed_name, $role);
   my $base_name = $composed_name.'::_BASE';
-  *{_getglob("${composed_name}::ISA")} = [ $base_name ];
+  # Not using _getglob, since setting @ISA via the typeglob breaks
+  # inheritance on 5.10.0 if the stash has previously been accessed an
+  # then a method called on the class (in that order!), which
+  # ->_install_methods (with the help of ->_install_does) ends up doing.
+  { no strict 'refs'; @{"${composed_name}::ISA"} = ( $base_name ); }
   my $modifiers = $INFO{$role}{modifiers}||[];
   my @mod_base;
   foreach my $modified (
@@ -250,7 +258,7 @@ sub _concrete_methods_of {
   my ($me, $role) = @_;
   my $info = $INFO{$role};
   # grab role symbol table
-  my $stash = do { no strict 'refs'; \%{"${role}::"}};
+  my $stash = _getstash($role);
   # reverse so our keys become the values (captured coderefs) in case
   # they got copied or re-used since
   my $not_methods = { reverse %{$info->{not_methods}||{}} };
@@ -258,8 +266,7 @@ sub _concrete_methods_of {
     # grab all code entries that aren't in the not_methods list
     map {
       my $code = *{$stash->{$_}}{CODE};
-      # rely on the '' key we added in import for "no code here"
-      exists $not_methods->{$code||''} ? () : ($_ => $code)
+      ( ! $code or exists $not_methods->{$code} ) ? () : ($_ => $code)
     } grep !ref($stash->{$_}), keys %$stash
   };
 }
@@ -278,7 +285,7 @@ sub _install_methods {
   my $methods = $me->_concrete_methods_of($role);
 
   # grab target symbol table
-  my $stash = do { no strict 'refs'; \%{"${to}::"}};
+  my $stash = _getstash($to);
 
   # determine already extant methods of target
   my %has_methods;
@@ -306,8 +313,17 @@ sub _install_modifiers {
   }
 }
 
+my $vcheck_error;
+
 sub _install_single_modifier {
   my ($me, @args) = @_;
+  defined($vcheck_error) or $vcheck_error = do {
+    local $@;
+    eval { Class::Method::Modifiers->VERSION(1.05); 1 }
+      ? 0
+      : $@
+  };
+  $vcheck_error and die $vcheck_error;
   Class::Method::Modifiers::install_modifier(@args);
 }
 
@@ -347,6 +363,8 @@ sub does_role {
 
 1;
 
+=encoding utf-8
+
 =head1 NAME
 
 Role::Tiny - Roles. Like a nouvelle cuisine portion size slice of Moose.
@@ -494,7 +512,7 @@ will work for classes but to test a role, one must use ::does_role directly.
 
 Additionally, Role::Tiny will override the standard Perl C<DOES> method
 for your class. However, if C<any> class in your class' inheritance
-heirarchy provides C<DOES>, then Role::Tiny will not override it.
+hierarchy provides C<DOES>, then Role::Tiny will not override it.
 
 =head1 METHODS