1 package MooseX::UndefTolerant::Constructor;
3 # applied to constructor method metaclass, for Moose < 1.9900
10 around _generate_slot_initializer => sub {
14 # note the key in the params may not match the attr name.
15 my $key_name = $self->_attributes->[$_[0]]->init_arg;
17 # insert a line of code at the start of the initializer,
18 # clearing the param if it's undefined.
20 if (defined $key_name)
22 # leave the value unscathed if the attribute's type constraint can
23 # handle undef (or doesn't have one, which implicitly means it can)
24 my $type_constraint = $self->_attributes->[$_[0]]->type_constraint;
25 if ($type_constraint and not $type_constraint->check(undef))
28 qq# delete \$params->{'$key_name'} unless # .
29 qq# exists \$params->{'$key_name'} && defined \$params->{'$key_name'};\n#;
31 return $tolerant_code . $self->$orig(@_);
35 return $self->$orig(@_);