From: Yuval Kogman Date: Sun, 27 Jan 2008 00:03:08 +0000 (+0000) Subject: init_arg can be undef X-Git-Tag: 0_37~17 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=849811463e6a7531a72f545a9a78902724b4085e;p=gitmo%2FMoose.git init_arg can be undef --- diff --git a/lib/Moose/Meta/Method/Constructor.pm b/lib/Moose/Meta/Method/Constructor.pm index 24c845d..caefb14 100644 --- a/lib/Moose/Meta/Method/Constructor.pm +++ b/lib/Moose/Meta/Method/Constructor.pm @@ -129,25 +129,28 @@ sub _generate_slot_initializer { my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME - if ($is_moose && $attr->is_required && !$attr->has_default && !$attr->has_builder) { + if ($is_moose && defined($attr->init_arg) && $attr->is_required && !$attr->has_default && !$attr->has_builder) { push @source => ('(exists $params{\'' . $attr->init_arg . '\'}) ' . '|| confess "Attribute (' . $attr->name . ') is required";'); } if (($attr->has_default || $attr->has_builder) && !($is_moose && $attr->is_lazy)) { - push @source => 'if (exists $params{\'' . $attr->init_arg . '\'}) {'; + if ( defined( my $init_arg = $attr->init_arg ) ) { + push @source => 'if (exists $params{\'' . $init_arg . '\'}) {'; - push @source => ('my $val = $params{\'' . $attr->init_arg . '\'};'); - if ($is_moose && $attr->has_type_constraint) { - if ($attr->should_coerce && $attr->type_constraint->has_coercion) { - push @source => $self->_generate_type_coercion($attr, '$type_constraints[' . $index . ']', '$val', '$val'); + push @source => ('my $val = $params{\'' . $init_arg . '\'};'); + + if ($is_moose && $attr->has_type_constraint) { + if ($attr->should_coerce && $attr->type_constraint->has_coercion) { + push @source => $self->_generate_type_coercion($attr, '$type_constraints[' . $index . ']', '$val', '$val'); + } + push @source => $self->_generate_type_constraint_check($attr, '$type_constraint_bodies[' . $index . ']', '$val'); } - push @source => $self->_generate_type_constraint_check($attr, '$type_constraint_bodies[' . $index . ']', '$val'); - } - push @source => $self->_generate_slot_assignment($attr, '$val'); + push @source => $self->_generate_slot_assignment($attr, '$val'); - push @source => "} else {"; + push @source => "} else {"; + } my $default; if ( $attr->has_default ) { @@ -165,12 +168,12 @@ sub _generate_slot_initializer { ) if ($is_moose && $attr->has_type_constraint); push @source => $self->_generate_slot_assignment($attr, $default); - push @source => "}"; + push @source => "}" if defined $attr->init_arg; } - else { - push @source => '(exists $params{\'' . $attr->init_arg . '\'}) && do {'; + elsif ( defined( my $init_arg = $attr->init_arg ) ) { + push @source => '(exists $params{\'' . $init_arg . '\'}) && do {'; - push @source => ('my $val = $params{\'' . $attr->init_arg . '\'};'); + push @source => ('my $val = $params{\'' . $init_arg . '\'};'); if ($is_moose && $attr->has_type_constraint) { if ($attr->should_coerce && $attr->type_constraint->has_coercion) { push @source => $self->_generate_type_coercion($attr, '$type_constraints[' . $index . ']', '$val', '$val'); diff --git a/t/300_immutable/004_inlined_constructors_n_types.t b/t/300_immutable/004_inlined_constructors_n_types.t index 54b3c66..7830752 100644 --- a/t/300_immutable/004_inlined_constructors_n_types.t +++ b/t/300_immutable/004_inlined_constructors_n_types.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 3; +use Test::More tests => 4; use Test::Exception; BEGIN { @@ -25,6 +25,7 @@ as with a Class::MOP::Attribute object. has 'foo' => (is => 'rw', isa => 'Int'); has 'baz' => (is => 'rw', isa => 'Int'); + has 'zot' => (is => 'rw', isa => 'Int', init_arg => undef); Foo->meta->add_attribute( Class::MOP::Attribute->new( @@ -38,9 +39,13 @@ as with a Class::MOP::Attribute object. } lives_ok { - Foo->new(foo => 10, bar => "Hello World", baz => 10); + Foo->new(foo => 10, bar => "Hello World", baz => 10, zot => 4); } '... this passes the constuctor correctly'; +lives_ok { + Foo->new(foo => 10, bar => "Hello World", baz => 10, zot => "not an int"); +} "... the constructor doesn't care about 'zot'"; + dies_ok { Foo->new(foo => "Hello World", bar => 100, baz => "Hello World"); } '... this fails the constuctor correctly';