From: Karen Etheridge Date: Sun, 23 Sep 2012 19:59:24 +0000 (-0700) Subject: do nothing at construction time for attributes with undef init_arg - fixes undefined... X-Git-Tag: v0.18~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMooseX-UndefTolerant.git;a=commitdiff_plain;h=0601096cb90ce43aa10165078a330de93c71e4ca do nothing at construction time for attributes with undef init_arg - fixes undefined warning --- diff --git a/Changes b/Changes index 11b8e3c..af6042f 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,8 @@ Revision history for MooseX-UndefTolerant {{$NEXT}} + * avoid undef warning by not attempting to manipulate constructor data for + init_arg => undef attributes (thanks Damon Tkoch for the report!) 0.17 2012-05-03 14:43:42 PDT-0700 * re-release to fix dependency issue *again*! :) diff --git a/lib/MooseX/UndefTolerant/Attribute.pm b/lib/MooseX/UndefTolerant/Attribute.pm index 9caabc7..644611e 100644 --- a/lib/MooseX/UndefTolerant/Attribute.pm +++ b/lib/MooseX/UndefTolerant/Attribute.pm @@ -11,7 +11,7 @@ around('initialize_instance_slot', sub { # If our parameter passed in was undef, remove it from the parameter list... # 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($params->{$key_name})) + if (defined $key_name and not defined($params->{$key_name})) { my $type_constraint = $self->type_constraint; if ($type_constraint and not $type_constraint->check(undef)) @@ -20,7 +20,7 @@ around('initialize_instance_slot', sub { } } - # Invoke the real init, as the above line cleared the undef + # Invoke the real init, as the above line cleared the undef param value $self->$orig(@_) }); diff --git a/t/undef_init_arg.t b/t/undef_init_arg.t new file mode 100644 index 0000000..7b3c579 --- /dev/null +++ b/t/undef_init_arg.t @@ -0,0 +1,31 @@ +use strict; +use warnings; + +use Test::More tests => 7; +use Test::Fatal; +use Test::Moose; +use Test::NoWarnings 0.94 ':early'; + +{ +package Foo; +use Moose; +use MooseX::UndefTolerant; + +has 'bar' => ( + is => 'ro', + isa => 'Num', + init_arg => undef, +); +} + +package main; + +with_immutable +{ + is(exception { my $foo = Foo->new }, undef, 'constructed with no args'); + + is(exception { my $foo = Foo->new(bar => undef) }, undef, 'constructed with undef value'); + + is(exception { my $foo = Foo->new(bar => 1234) }, undef, 'constructed with defined value'); +} 'Foo'; +