X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FUndefTolerant%2FConstructor.pm;h=b9ccc0b03b1882d505e98988059c94a5275cb9cb;hb=d6ce838b149668ac59c654aa996c3915c311507c;hp=47d81283c857615c8a5846794279d4b4bc094e85;hpb=d10a4d7f63850b562f9c5267707b9072b84d33f3;p=gitmo%2FMooseX-UndefTolerant.git diff --git a/lib/MooseX/UndefTolerant/Constructor.pm b/lib/MooseX/UndefTolerant/Constructor.pm index 47d8128..b9ccc0b 100644 --- a/lib/MooseX/UndefTolerant/Constructor.pm +++ b/lib/MooseX/UndefTolerant/Constructor.pm @@ -1,25 +1,29 @@ package MooseX::UndefTolerant::Constructor; use Moose::Role; -around('_generate_slot_initializer', sub { - my $orig = shift; - my $self = shift; - my $attr = $self->_attributes->[$_[0]]->init_arg; +if ( $Moose::VERSION < 1.9900 ) { + around('_generate_slot_initializer', sub { + my $orig = shift; + my $self = shift; - # insert a line of code at the start of the initializer, - # clearing the param if it's undefined. + # note the key in the params may not match the attr name. + my $key_name = $self->_attributes->[$_[0]]->init_arg; - if (defined $attr) { - my $tolerant_code = - qq# delete \$params->{'$attr'} unless # . - qq# exists \$params->{'$attr'} && defined \$params->{'$attr'};\n#; + # insert a line of code at the start of the initializer, + # clearing the param if it's undefined. - return $tolerant_code . $self->$orig(@_); - } - else { - return $self->$orig(@_); - } -}); + if (defined $key_name) { + 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(@_); + } + }); +} no Moose::Role;