1 package MooseX::UndefTolerant::Attribute;
4 around('initialize_instance_slot', sub {
7 my ($meta_instance, $instance, $params) = @_;
9 my $key_name = $self->init_arg;
11 # If our parameter passed in was undef, remove it from the parameter list...
12 # but leave the value unscathed if the attribute's type constraint can
13 # handle undef (or doesn't have one, which implicitly means it can)
14 if (defined $key_name and not defined($params->{$key_name}))
16 my $type_constraint = $self->type_constraint;
17 if ($type_constraint and not $type_constraint->check(undef))
19 delete $params->{$key_name};
23 # Invoke the real init, as the above line cleared the undef param value
29 # ABSTRACT: Make your attribute(s) tolerant to undef intitialization
38 use MooseX::UndefTolerant::Attribute;
41 traits => [ qw(MooseX::UndefTolerant::Attribute)],
44 predicate => 'has_bar'
47 # Meanwhile, under the city...
50 my $class = My::Class->new(bar => undef);
51 $class->has_bar # False!
55 Applying this trait to your attribute makes it's initialization tolerant of
56 of undef. If you specify the value of undef to any of the attributes they
57 will not be initialized (or will be set to the default, if applicable).
58 Effectively behaving as if you had not provided a value at all.