From: Tokuhiro Matsuno Date: Sat, 4 Apr 2009 16:12:58 +0000 (+0900) Subject: do not create a copy of argument, if not needed X-Git-Tag: 0.20~15 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=55fc05e68941ba0d6f321e53d1ba6550f0769bac;p=gitmo%2FMouse.git do not create a copy of argument, if not needed --- diff --git a/lib/Mouse/Meta/Method/Accessor.pm b/lib/Mouse/Meta/Method/Accessor.pm index ba46cbd..af49197 100644 --- a/lib/Mouse/Meta/Method/Accessor.pm +++ b/lib/Mouse/Meta/Method/Accessor.pm @@ -27,36 +27,33 @@ sub generate_accessor_method_inline { if ($attribute->_is_metadata eq 'rw') { $accessor .= '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" . - 'if (@_ >= 2) {' . "\n"; + 'if (scalar(@_) >= 2) {' . "\n"; my $value = '$_[1]'; if ($constraint) { - $accessor .= 'my $val = '; if ($should_coerce) { $accessor .= "\n". '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" . - 'Mouse::Util::TypeConstraints->typecast_constraints("'.$attribute->associated_class->name.'", $attribute->{type_constraint}, '.$value.');'; - } else { - $accessor .= $value.';'; + 'my $val = Mouse::Util::TypeConstraints->typecast_constraints("'.$attribute->associated_class->name.'", $attribute->{type_constraint}, '.$value.');'; + $value = '$val'; } if ($compiled_type_constraint) { $accessor .= "\n". '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" . - 'unless ($compiled_type_constraint->($val)) { - $attribute->verify_type_constraint_error($name, $val, $attribute->{type_constraint}); + 'unless ($compiled_type_constraint->('.$value.')) { + $attribute->verify_type_constraint_error($name, '.$value.', $attribute->{type_constraint}); }' . "\n"; } else { $accessor .= "\n". '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" . - 'unless ($constraint->check($val)) { - $attribute->verify_type_constraint_error($name, $val, $attribute->{type_constraint}); + 'unless ($constraint->check('.$value.')) { + $attribute->verify_type_constraint_error($name, '.$value.', $attribute->{type_constraint}); }' . "\n"; } - $value = '$val'; } # if there's nothing left to do for the attribute we can return during