fixed: immutable class with undef-tolerant TC should not have its undef value strippe...
[gitmo/MooseX-UndefTolerant.git] / lib / MooseX / UndefTolerant / Class.pm
index 13d1380..1e5e91c 100644 (file)
@@ -18,13 +18,19 @@ around _inline_init_attr_from_constructor => sub {
     my @source = $self->$orig(@_);
 
     my $init_arg = $attr->init_arg;
-
-    return
-        "if ( exists \$params->{$init_arg} && defined \$params->{$init_arg} ) {",
-            @source,
+    my $type_constraint = $attr->type_constraint;
+    my $tc_says_clean = ($type_constraint && !$type_constraint->check(undef) ? 1 : 0);
+
+    return ($tc_says_clean ? (
+        "if ( exists \$params->{'$init_arg'} && defined \$params->{'$init_arg'} ) {",
+        ) : (),
+        @source,
+        $tc_says_clean ? (
         '} else {',
-            "delete \$params->{$init_arg};",
-        '}';
+            "delete \$params->{'$init_arg'};",
+        '}',
+        ) : (),
+    );
 };
 
 no Moose::Role;