my $default;
if (!exists($args{$key})) {
- if (exists($attribute->{default})) {
+ if (exists($attribute->{default}) || exists($attribute->{builder})) {
unless ($attribute->{lazy}) {
- if (ref($attribute->{default}) eq 'CODE') {
- $instance->{$key} = $attribute->{default}->();
- Scalar::Util::weaken($instance->{$key})
- if $attribute->{weak_ref};
- }
- else {
- $instance->{$key} = $attribute->{default};
- Scalar::Util::weaken($instance->{$key})
- if $attribute->{weak_ref};
- }
+ my $builder = $attribute->{builder};
+ my $default = exists($attribute->{builder})
+ ? $instance->$builder
+ : ref($attribute->{default}) eq 'CODE'
+ ? $attribute->{default}->()
+ : $attribute->{default};
+
+ $attribute->verify_type_constraint($default)
+ if $attribute->has_type_constraint;
+
+ $instance->{$key} = $default;
+
+ Scalar::Util::weaken($instance->{$key})
+ if $attribute->{weak_ref};
}
}
else {
}
if (exists($args{$key})) {
+ $attribute->verify_type_constraint($args{$key})
+ if $attribute->has_type_constraint;
+
$instance->{$key} = $args{$key};
+
Scalar::Util::weaken($instance->{$key})
if $attribute->{weak_ref};