From: Chris Andrews Date: Sat, 9 Oct 2010 10:50:49 +0000 (+0100) Subject: Be more careful avoiding warnings when the init_arg is undef. X-Git-Tag: 0.06~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d10a4d7f63850b562f9c5267707b9072b84d33f3;p=gitmo%2FMooseX-UndefTolerant.git Be more careful avoiding warnings when the init_arg is undef. --- diff --git a/lib/MooseX/UndefTolerant/Attribute.pm b/lib/MooseX/UndefTolerant/Attribute.pm index c5c3579..970b70b 100644 --- a/lib/MooseX/UndefTolerant/Attribute.pm +++ b/lib/MooseX/UndefTolerant/Attribute.pm @@ -9,7 +9,7 @@ around('initialize_instance_slot', sub { # $_[2] is the hashref of options passed to the constructor. If our # parameter passed in was undef, pop it off the args... - pop unless (exists($_[2]->{$ia}) && defined($_[2]->{$ia})); + pop unless (defined $ia && exists($_[2]->{$ia}) && defined($_[2]->{$ia})); # Invoke the real init, as the above line cleared the unef $self->$orig(@_) diff --git a/lib/MooseX/UndefTolerant/Constructor.pm b/lib/MooseX/UndefTolerant/Constructor.pm index e89b609..47d8128 100644 --- a/lib/MooseX/UndefTolerant/Constructor.pm +++ b/lib/MooseX/UndefTolerant/Constructor.pm @@ -6,11 +6,19 @@ around('_generate_slot_initializer', sub { my $self = shift; my $attr = $self->_attributes->[$_[0]]->init_arg; - 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(@_); + if (defined $attr) { + my $tolerant_code = + qq# delete \$params->{'$attr'} unless # . + qq# exists \$params->{'$attr'} && defined \$params->{'$attr'};\n#; + + return $tolerant_code . $self->$orig(@_); + } + else { + return $self->$orig(@_); + } }); no Moose::Role;