Close over $key so we don't need to do an assignment or variable lookup in the constr...
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Constructor.pm
index b1db7d4..32baa00 100644 (file)
@@ -5,9 +5,10 @@ use warnings;
 sub generate_constructor_method_inline {
     my ($class, $meta) = @_;
 
+    my @attrs = $meta->compute_all_applicable_attributes; # this one is using by evaled code
     my $buildall = $class->_generate_BUILDALL($meta);
     my $buildargs = $class->_generate_BUILDARGS();
-    my $processattrs = $class->_generate_processattrs($meta);
+    my $processattrs = $class->_generate_processattrs($meta, \@attrs);
 
     my $code = <<"...";
     sub {
@@ -29,10 +30,10 @@ sub generate_constructor_method_inline {
 }
 
 sub _generate_processattrs {
-    my ($class, $meta, ) = @_;
-    my @attrs = $meta->compute_all_applicable_attributes;
+    my ($class, $meta, $attrs) = @_;
     my @res;
-    for my $attr (@attrs) {
+    for my $index (0..scalar(@$attrs)-1) {
+        my $attr = $attrs->[$index];
         my $from = $attr->init_arg;
         my $key  = $attr->name;
         my $part1 = do {
@@ -43,9 +44,9 @@ sub _generate_processattrs {
             if ($attr->has_type_constraint) {
                 push @code, "\$attr->verify_type_constraint( \$args->{\$from} );";
             }
-            push @code, "\$instance->{\$key} = \$args->{\$from};";
+            push @code, "\$instance->{'$key'} = \$args->{\$from};";
             if ($attr->is_weak_ref) {
-                push @code, "weaken( \$instance->{\$key} ) if ref( \$instance->{\$key} );";
+                push @code, "weaken( \$instance->{'$key'} ) if ref( \$instance->{'$key'} );";
             }
             if ( $attr->has_trigger ) {
                 push @code, "\$attr->trigger->( \$instance, \$args->{\$from}, \$attr );";
@@ -71,9 +72,9 @@ sub _generate_processattrs {
                     if ($attr->has_type_constraint) {
                         push @code, "\$attr->verify_type_constraint(\$value);";
                     }
-                    push @code, "\$instance->{\$key} = \$value;";
+                    push @code, "\$instance->{'$key'} = \$value;";
                     if ($attr->is_weak_ref) {
-                        push @code, "weaken( \$instance->{\$key} ) if ref( \$instance->{\$key} );";
+                        push @code, "weaken( \$instance->{'$key'} ) if ref( \$instance->{'$key'} );";
                     }
                 }
                 join "\n", @code;
@@ -88,9 +89,8 @@ sub _generate_processattrs {
         };
         my $code = <<"...";
             {
-                my \$attr = \$meta->get_attribute('$key');
+                my \$attr = \$attrs[$index];
                 my \$from = '$from';
-                my \$key  = '$key';
                 if (defined(\$from) && exists(\$args->{\$from})) {
                     $part1;
                 } else {