X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FUndefTolerant%2FConstructor.pm;fp=lib%2FMooseX%2FUndefTolerant%2FConstructor.pm;h=969facab9ddce5a238b0228faa4e8bb447d2eb42;hb=3991a5d2d6058c15d85b028dae0c548922c38dbe;hp=2463bf25c66cbcb3fb3e40ef31b0ccf5fe8efefa;hpb=015b91679983f142397cd7172262a5e56f3cd850;p=gitmo%2FMooseX-UndefTolerant.git diff --git a/lib/MooseX/UndefTolerant/Constructor.pm b/lib/MooseX/UndefTolerant/Constructor.pm index 2463bf2..969faca 100644 --- a/lib/MooseX/UndefTolerant/Constructor.pm +++ b/lib/MooseX/UndefTolerant/Constructor.pm @@ -12,18 +12,23 @@ if ( $Moose::VERSION < 1.9900 ) { # insert a line of code at the start of the initializer, # clearing the param if it's undefined. - if (defined $key_name) { - my $tolerant_code = - qq# delete \$params->{'$key_name'} unless # . + if (defined $key_name) + { + # leave the value unscathed if the attribute's type constraint can + # handle undef (or doesn't have one, which implicitly means it can) + my $type_constraint = $self->_attributes->[$_[0]]->type_constraint; + if ($type_constraint and not $type_constraint->check(undef)) + { + my $tolerant_code = + qq# delete \$params->{'$key_name'} unless # . qq# exists \$params->{'$key_name'} && defined \$params->{'$key_name'};\n#; return $tolerant_code . $self->$orig(@_); + } } - else { - return $self->$orig(@_); - } - }); -} + + return $self->$orig(@_); +}); no Moose::Role;