improve wording - the bad code is actually _inline_slot_initializer
[gitmo/MooseX-UndefTolerant.git] / lib / MooseX / UndefTolerant / Attribute.pm
CommitLineData
5447ee45 1package MooseX::UndefTolerant::Attribute;
2use Moose::Role;
3
efcfddbd 4around('initialize_instance_slot', sub {
5447ee45 5 my $orig = shift;
6 my $self = shift;
7
3991a5d2 8 my $key_name = $self->init_arg;
9
10 # $_[2] is the hashref of options passed to the constructor.
11 # If our parameter passed in was undef, pop it off the args...
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 (not defined $key_name or not defined($_[2]->{$key_name}))
15 {
16 my $type_constraint = $self->type_constraint;
17 if ($type_constraint and not $type_constraint->check(undef))
18 {
19 pop;
20 }
21 }
22
23 # Invoke the real init, as the above line cleared the undef
5447ee45 24 $self->$orig(@_)
25});
26
19258058 271;
28
b2c5b43c 29# ABSTRACT: Make your attribute(s) tolerant to undef intitialization
19258058 30
b2c5b43c 31__END__
19258058 32
33=head1 SYNOPSIS
34
35 package My:Class;
36 use Moose;
37
38 use MooseX::UndefTolerant::Attribute;
39
40 has 'bar' => (
41 traits => [ qw(MooseX::UndefTolerant::Attribute)],
42 is => 'ro',
43 isa => 'Num',
44 predicate => 'has_bar'
45 );
46
47 # Meanwhile, under the city...
48
49 # Doesn't explode
50 my $class = My::Class->new(bar => undef);
51 $class->has_bar # False!
52
53=head1 DESCRIPTION
54
55Applying this trait to your attribute makes it's initialization tolerant of
56of undef. If you specify the value of undef to any of the attributes they
b2c5b43c 57will not be initialized (or will be set to the default, if applicable).
779ca481 58Effectively behaving as if you had not provided a value at all.
19258058 59
19258058 60=cut