make auto_deref also handles isa not only ArrayRef and HashRef, but also ArrayRef...
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Accessor.pm
index ba46cbd..38531bc 100644 (file)
@@ -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
@@ -91,7 +88,7 @@ sub generate_accessor_method_inline {
     }
 
     if ($should_deref) {
-        if (ref($constraint) && $constraint->name eq 'ArrayRef') {
+        if (ref($constraint) && $constraint->name =~ '^ArrayRef\b') {
             $accessor .= 'if (wantarray) {
                 return @{ '.$self.'->{'.$key.'} || [] };
             }';