my $instance = bless {}, $class;
for my $attribute (values %{ $class->meta->get_attribute_map }) {
- my $key = $attribute->name;
+ my $from = $attribute->init_arg;
+ my $key = $attribute->name;
my $default;
- if (!exists($args{$key})) {
+ if (!exists($args{$from})) {
if ($attribute->has_default || $attribute->has_builder) {
unless ($attribute->is_lazy) {
my $default = $attribute->default;
}
}
- if (exists($args{$key})) {
- $attribute->verify_type_constraint($args{$key})
+ if (exists($args{$from})) {
+ $attribute->verify_type_constraint($args{$from})
if $attribute->has_type_constraint;
- $instance->{$key} = $args{$key};
+ $instance->{$key} = $args{$from};
weaken($instance->{$key})
if ref($instance->{$key}) && $attribute->is_weak_ref;
if ($attribute->has_trigger) {
- $attribute->trigger->($instance, $args{$key}, $attribute);
+ $attribute->trigger->($instance, $args{$from}, $attribute);
}
}
}
is($object->{name}, 'default', 'value is in object->{name}');
my $object2 = Class->new(name => 'name', key => 'key');
-is($object2->name, 'name', 'attribute value is from name');
+is($object2->name, 'key', 'attribute value is from name');
is($object2->{key}, undef, 'no value for the init_arg');
-is($object2->{name}, 'name', 'value is in key from name');
+is($object2->{name}, 'key', 'value is in key from name');
my $attr = $object2->meta->get_attribute('name');
ok($attr, 'got the attribute object by name (not init_arg)');