X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FUndefTolerant%2FAttribute.pm;h=aa8f5d1f5041080f235a508ab2eae70edc346fc6;hb=3991a5d2d6058c15d85b028dae0c548922c38dbe;hp=15d35ddd2c73858047106662191be2ab79d6c3d6;hpb=015b91679983f142397cd7172262a5e56f3cd850;p=gitmo%2FMooseX-UndefTolerant.git diff --git a/lib/MooseX/UndefTolerant/Attribute.pm b/lib/MooseX/UndefTolerant/Attribute.pm index 15d35dd..aa8f5d1 100644 --- a/lib/MooseX/UndefTolerant/Attribute.pm +++ b/lib/MooseX/UndefTolerant/Attribute.pm @@ -5,13 +5,22 @@ around('initialize_instance_slot', sub { my $orig = shift; my $self = shift; - my $ia = $self->init_arg; - - # $_[2] is the hashref of options passed to the constructor. If our - # parameter passed in was undef, pop it off the args... - pop unless (defined $ia && defined($_[2]->{$ia})); - - # Invoke the real init, as the above line cleared the unef + my $key_name = $self->init_arg; + + # $_[2] is the hashref of options passed to the constructor. + # If our parameter passed in was undef, pop it off the args... + # but leave the value unscathed if the attribute's type constraint can + # handle undef (or doesn't have one, which implicitly means it can) + if (not defined $key_name or not defined($_[2]->{$key_name})) + { + my $type_constraint = $self->type_constraint; + if ($type_constraint and not $type_constraint->check(undef)) + { + pop; + } + } + + # Invoke the real init, as the above line cleared the undef $self->$orig(@_) });