X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FUndefTolerant%2FClass.pm;h=a2436054720efbf4c9eae98c7d2d13906d411e3f;hb=72f76bd18a4e11dd3edd2b581cb6b0e0898bcc56;hp=3aa9ac0fb86dc0aebb24e7b2457194e99af9e8ab;hpb=02e25b000b1a4ed8779f538eb35861336ff6b53f;p=gitmo%2FMooseX-UndefTolerant.git diff --git a/lib/MooseX/UndefTolerant/Class.pm b/lib/MooseX/UndefTolerant/Class.pm index 3aa9ac0..a243605 100644 --- a/lib/MooseX/UndefTolerant/Class.pm +++ b/lib/MooseX/UndefTolerant/Class.pm @@ -7,6 +7,9 @@ use warnings; use Moose::Role; +# TODO: this code should be in the attribute trait, in the inlined version of +# initialize_instance_slot, but this does not yet exist! + around _inline_init_attr_from_constructor => sub { my $orig = shift; my $self = shift; @@ -15,13 +18,20 @@ 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); + + # FIXME: not properly sanitizing field names - e.g. consider a field name "Z'ha'dum" + 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;