From: Yuval Kogman Date: Sat, 26 Jan 2008 23:53:58 +0000 (+0000) Subject: init_arg can be undef X-Git-Tag: 0_37~18 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=625d571f2f24cecb72ed3e1015c47cc7c9503856;p=gitmo%2FMoose.git init_arg can be undef --- diff --git a/lib/Moose/Meta/Attribute.pm b/lib/Moose/Meta/Attribute.pm index 28c0b8b..39036f4 100644 --- a/lib/Moose/Meta/Attribute.pm +++ b/lib/Moose/Meta/Attribute.pm @@ -208,7 +208,7 @@ sub initialize_instance_slot { my $val; my $value_is_set; - if (exists $params->{$init_arg}) { + if ( defined($init_arg) and exists $params->{$init_arg}) { $val = $params->{$init_arg}; $value_is_set = 1; } diff --git a/lib/Moose/Meta/Class.pm b/lib/Moose/Meta/Class.pm index 928cd18..be298e7 100644 --- a/lib/Moose/Meta/Class.pm +++ b/lib/Moose/Meta/Class.pm @@ -80,12 +80,11 @@ sub new_object { my ($class, %params) = @_; my $self = $class->SUPER::new_object(%params); foreach my $attr ($class->compute_all_applicable_attributes()) { - # FIXME: - # this does not accept undefined - # values, nor does it accept false - # values to be passed into the init-arg - next unless $params{$attr->init_arg} && $attr->can('has_trigger') && $attr->has_trigger; - $attr->trigger->($self, $params{$attr->init_arg}, $attr); + if ( defined( my $init_arg = $attr->init_arg ) ) { + if ( exists($params{$init_arg}) && $attr->can('has_trigger') && $attr->has_trigger ) { + $attr->trigger->($self, $params{$init_arg}, $attr); + } + } } return $self; } diff --git a/t/020_attributes/018_no_init_arg.t b/t/020_attributes/018_no_init_arg.t new file mode 100644 index 0000000..ae714ec --- /dev/null +++ b/t/020_attributes/018_no_init_arg.t @@ -0,0 +1,35 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 5; +use Test::Exception; + +BEGIN { + use_ok('Moose'); +} + +{ + package Foo; + use Moose; + + eval { + has 'foo' => ( + is => "rw", + init_arg => undef, + ); + }; + ::ok(!$@, '... created the attr okay'); +} + +{ + my $foo = Foo->new( foo => "bar" ); + isa_ok($foo, 'Foo'); + + is( $foo->foo, undef, "field is not set via init arg" ); + + $foo->foo("blah"); + + is( $foo->foo, "blah", "field is set via setter" ); +}