Don't use $_ as loop variable when calling arbitrary code (RT#81072)
Dagfinn Ilmari Mannsåker [Fri, 9 Nov 2012 16:35:06 +0000 (16:35 +0000)]
Changes
lib/Moo.pm
lib/Moo/Role.pm
t/global_underscore.t [new file with mode: 0644]
t/lib/ClobberUnderscore.pm [new file with mode: 0644]
t/lib/UnderscoreClass.pm [new file with mode: 0644]
t/lib/UnderscoreRole.pm [new file with mode: 0644]

diff --git a/Changes b/Changes
index eb52edf..b8cae11 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,3 +1,5 @@
+  - Don't use $_ as loop variable when calling arbitrary code (RT#81072)
+
 1.000005 - 2012-10-23
   - fix POD typo (RT#80060)
   - include init_arg name in constructor errors (RT#79596)
index c47c3e9..fdd46e2 100644 (file)
@@ -75,11 +75,11 @@ sub unimport {
 sub _set_superclasses {
   my $class = shift;
   my $target = shift;
-  for (@_) {
-    _load_module($_);
-    if ($INC{"Role/Tiny.pm"} && $Role::Tiny::INFO{$_}) {
+  foreach my $superclass (@_) {
+    _load_module($superclass);
+    if ($INC{"Role/Tiny.pm"} && $Role::Tiny::INFO{$superclass}) {
       require Carp;
-      Carp::croak("Can't extend role '$_'");
+      Carp::croak("Can't extend role '$superclass'");
     }
   }
   # Can't do *{...} = \@_ or 5.10.0's mro.pm stops seeing @ISA
index c382dd1..61d66da 100644 (file)
@@ -190,7 +190,9 @@ sub _make_accessors {
 
 sub apply_roles_to_package {
   my ($me, $to, @roles) = @_;
-  $me->_inhale_if_moose($_) for @roles;
+  foreach my $role (@roles) {
+      $me->_inhale_if_moose($role);
+  }
   $me->SUPER::apply_roles_to_package($to, @roles);
 }
 
@@ -211,7 +213,9 @@ sub create_class_with_roles {
 
   return $new_name if $Role::Tiny::COMPOSED{class}{$new_name};
 
-  $me->_inhale_if_moose($_) for @roles;
+  foreach my $role (@roles) {
+      $me->_inhale_if_moose($role);
+  }
 
   my $m;
   if ($INC{"Moo.pm"}
diff --git a/t/global_underscore.t b/t/global_underscore.t
new file mode 100644 (file)
index 0000000..da11492
--- /dev/null
@@ -0,0 +1,23 @@
+use strict;
+use warnings;
+use Test::More;
+use lib qw(t/lib);
+
+use_ok('UnderscoreClass');
+
+is(
+       UnderscoreClass->c1,
+       'c1',
+);
+
+is(
+       UnderscoreClass->r1,
+       'r1',
+);
+
+is(
+       ClobberUnderscore::h1(),
+       'h1',
+);
+
+done_testing;
diff --git a/t/lib/ClobberUnderscore.pm b/t/lib/ClobberUnderscore.pm
new file mode 100644 (file)
index 0000000..2679278
--- /dev/null
@@ -0,0 +1,4 @@
+package ClobberUnderscore;
+sub h1 { 'h1' };
+undef $_;
+1;
diff --git a/t/lib/UnderscoreClass.pm b/t/lib/UnderscoreClass.pm
new file mode 100644 (file)
index 0000000..980b862
--- /dev/null
@@ -0,0 +1,5 @@
+package UnderscoreClass;
+use Moo;
+with qw(UnderscoreRole);
+sub c1 { 'c1' };
+1;
diff --git a/t/lib/UnderscoreRole.pm b/t/lib/UnderscoreRole.pm
new file mode 100644 (file)
index 0000000..3afe0c9
--- /dev/null
@@ -0,0 +1,5 @@
+package UnderscoreRole;
+use Moo::Role;
+use ClobberUnderscore;
+sub r1 { 'r1' };
+1;