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 ) {
) 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');
use strict;
use warnings;
-use Test::More tests => 3;
+use Test::More tests => 4;
use Test::Exception;
BEGIN {
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(
}
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';