Use fewer assignments when doing a coercion in the constructor
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Constructor.pm
index 84d36ee..c0cf03e 100644 (file)
@@ -3,89 +3,112 @@ use strict;
 use warnings;
 
 sub generate_constructor_method_inline {
-    my ($class, $meta) = @_; 
-    my $code = $class->_generate_constructor_method_inline($meta);
-    warn $code if $ENV{DEBUG};
-
-    local $@;
-    my $res = eval $code;
-    die $@ if $@;
-    $res;
-}
-
-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 $classname = $meta->name;
-    my $processattrs = $class->_generate_processattrs($meta);
+    my $processattrs = $class->_generate_processattrs($meta, \@attrs);
 
-    return <<"...";
+    my $code = <<"...";
     sub {
         my \$class = shift;
         my \$args = $buildargs;
-        my \$instance = bless {}, '$classname';
-        my \$meta = \$instance->meta;
+        my \$instance = bless {}, \$class;
         $processattrs;
         $buildall;
         return \$instance;
     }
 ...
+
+    warn $code if $ENV{DEBUG};
+
+    local $@;
+    my $res = eval $code;
+    die $@ if $@;
+    $res;
 }
 
 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 {
             my @code;
+
+            push @code, "my \$value = \$args->{'$from'};";
+
             if ($attr->should_coerce) {
-                push @code, "\$args->{\$from} = \$attr->coerce_constraint( \$args->{\$from} );";
+                push @code, "\$value = \$attr->coerce_constraint( \$value );";
             }
+
             if ($attr->has_type_constraint) {
-                push @code, "\$attr->verify_type_constraint( \$args->{\$from} );";
+                push @code, "\$attr->verify_type_constraint( \$value );";
             }
-            push @code, "\$instance->{\$key} = \$args->{\$from};";
+
+            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( \$value );";
             }
+
             if ( $attr->has_trigger ) {
-                push @code, "\$attr->trigger->( \$instance, \$args->{\$from}, \$attr );";
+                push @code, "\$attr->trigger->( \$instance, \$value, \$attr );";
             }
+
             join "\n", @code;
         };
+
         my $part2 = do {
             my @code;
+
             if ( $attr->has_default || $attr->has_builder ) {
                 unless ( $attr->is_lazy ) {
                     my $default = $attr->default;
                     my $builder = $attr->builder;
-                    if ($attr->has_builder) {
-                        push @code, "my \$value = \$instance->$builder;";
-                    } elsif (ref($default) eq 'CODE') {
-                        push @code, "my \$value = \$attr->default()->();";
-                    } else {
-                        push @code, "my \$value = \$attr->default();";
+
+                    push @code, "my \$value = ";
+
+                    if ($attr->should_coerce) {
+                        push @code, "\$attr->coerce_constraint(";
                     }
+
+                        if ($attr->has_builder) {
+                            push @code, "\$instance->$builder";
+                        }
+                        elsif (ref($default) eq 'CODE') {
+                            push @code, "\$attr->default()->()";
+                        }
+                        else {
+                            push @code, "\$attr->default()";
+                        }
+
                     if ($attr->should_coerce) {
-                        push @code, "\$value = \$attr->coerce_constraint(\$value);";
+                        push @code, ");";
                     }
+                    else {
+                        push @code, ";";
+                    }
+
                     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( \$value );";
                     }
                 }
                 join "\n", @code;
             }
             else {
                 if ( $attr->is_required ) {
-                    q{Carp::confess("Attribute (} . $attr->name . q{) is required");};
+                    qq{Carp::confess("Attribute ($key) is required");};
                 } else {
                     ""
                 }
@@ -93,10 +116,8 @@ sub _generate_processattrs {
         };
         my $code = <<"...";
             {
-                my \$attr = \$meta->get_attribute('$key');
-                my \$from = '$from';
-                my \$key  = '$key';
-                if (defined(\$from) && exists(\$args->{\$from})) {
+                my \$attr = \$attrs[$index];
+                if (exists(\$args->{'$from'})) {
                     $part1;
                 } else {
                     $part2;