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;
}
--- /dev/null
+#!/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" );
+}