Don't use $_ as loop variable when calling arbitrary code (RT#81072)
[gitmo/Moo.git] / lib / Moo / Role.pm
index 256b191..61d66da 100644 (file)
@@ -25,13 +25,17 @@ sub import {
   # get symbol table reference
   my $stash = do { no strict 'refs'; \%{"${target}::"} };
   _install_tracked $target => has => sub {
-    my ($name, %spec) = @_;
-    ($INFO{$target}{accessor_maker} ||= do {
-      require Method::Generate::Accessor;
-      Method::Generate::Accessor->new
-    })->generate_method($target, $name, \%spec);
-    push @{$INFO{$target}{attributes}||=[]}, $name, \%spec;
-    $me->_maybe_reset_handlemoose($target);
+    my ($name_proto, %spec) = @_;
+    my $name_isref = ref $name_proto eq 'ARRAY';
+    foreach my $name ($name_isref ? @$name_proto : $name_proto) {
+      my $spec_ref = $name_isref ? +{%spec} : \%spec;
+      ($INFO{$target}{accessor_maker} ||= do {
+        require Method::Generate::Accessor;
+        Method::Generate::Accessor->new
+      })->generate_method($target, $name, $spec_ref);
+      push @{$INFO{$target}{attributes}||=[]}, $name, $spec_ref;
+      $me->_maybe_reset_handlemoose($target);
+    }
   };
   # install before/after/around subs
   foreach my $type (qw(before after around)) {
@@ -101,13 +105,30 @@ sub _inhale_if_moose {
     $INFO{$role}{attributes} = [
       map +($_ => do {
         my $spec = { %{$meta->get_attribute($_)} };
+
         if ($spec->{isa}) {
-          require Moose::Util::TypeConstraints;
-          my $tc = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($spec->{isa});
+
+          my $get_constraint = do {
+            my $pkg = $meta->isa('Mouse::Meta::Role')
+                        ? 'Mouse::Util::TypeConstraints'
+                        : 'Moose::Util::TypeConstraints';
+            _load_module($pkg);
+            $pkg->can('find_or_create_isa_type_constraint');
+          };
+
+          my $tc = $get_constraint->($spec->{isa});
           my $check = $tc->_compiled_type_constraint;
-          $spec->{isa} = sub { &$check or die "Type constraint failed for $_[0]" };
+
+          $spec->{isa} = sub {
+            &$check or die "Type constraint failed for $_[0]"
+          };
+
           if ($spec->{coerce}) {
-             $spec->{coerce} = $tc->coercion->_compiled_type_coercion;
+
+             # Mouse has _compiled_type_coercion straight on the TC object
+             $spec->{coerce} = $tc->${\(
+               $tc->can('coercion')||sub { $_[0] }
+             )}->_compiled_type_coercion;
           }
         }
         $spec;
@@ -169,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);
 }
 
@@ -190,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"}
@@ -269,7 +294,7 @@ Moo::Role - Minimal Object Orientation support for Roles
 
  1;
 
-else where
+And elsewhere:
 
  package Some::Class;