From: Shawn M Moore Date: Wed, 3 Dec 2008 03:55:53 +0000 (+0000) Subject: Use fewer assignments when doing a coercion in the constructor X-Git-Tag: 0.19~136^2~69 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e8ba7b267968d4ca0e94d03276ca4c33fed40482;p=gitmo%2FMouse.git Use fewer assignments when doing a coercion in the constructor --- diff --git a/lib/Mouse/Meta/Method/Constructor.pm b/lib/Mouse/Meta/Method/Constructor.pm index 13c4ad0..c0cf03e 100644 --- a/lib/Mouse/Meta/Method/Constructor.pm +++ b/lib/Mouse/Meta/Method/Constructor.pm @@ -65,26 +65,43 @@ sub _generate_processattrs { 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;"; + 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;