1 package MooseX::UndefTolerant;
6 use MooseX::UndefTolerant::Attribute;
7 use MooseX::UndefTolerant::Class;
8 use MooseX::UndefTolerant::Constructor;
13 attribute => [ 'MooseX::UndefTolerant::Attribute' ],
16 if ( $Moose::VERSION < 1.9900 ) {
17 $metaroles{class_metaroles}{constructor} = [
18 'MooseX::UndefTolerant::Constructor',
22 $metaroles{class_metaroles}{class} = [
23 'MooseX::UndefTolerant::Class',
25 $metaroles{role_metaroles} = {
26 applied_attribute => [
27 'MooseX::UndefTolerant::Attribute',
30 'MooseX::UndefTolerant::Role',
32 application_to_class => [
33 'MooseX::UndefTolerant::ApplicationToClass',
35 application_to_role => [
36 'MooseX::UndefTolerant::ApplicationToRole',
42 Moose::Exporter->setup_import_methods(%metaroles);
46 # ABSTRACT: Make your attribute(s) tolerant to undef initialization
55 use MooseX::UndefTolerant;
60 predicate => 'has_name'
63 # Meanwhile, under the city...
66 my $class = My::Class->new(name => undef);
67 $class->has_name # False!
69 Or, if you only want one attribute to have this behaviour:
74 use MooseX::UndefTolerant::Attribute;
77 traits => [ qw(MooseX::UndefTolerant::Attribute)],
80 predicate => 'has_bar'
85 Loading this module in your L<Moose> class makes initialization of your
86 attributes tolerant of undef. If you specify the value of undef to any of
87 the attributes they will not be initialized, effectively behaving as if you
88 had not provided a value at all.
90 You can also apply the 'UndefTolerant' trait to individual attributes. See
91 L<MooseX::UndefTolerant::Attribute> for details.
93 There will be no change in behaviour to any attribute with a type constraint
94 that accepts undef values (for example C<Maybe> types), as it is presumed that
95 since the type is already "undef tolerant", there is no need to avoid
96 initializing the attribute value with C<undef>.
98 As of Moose 1.9900, this module can also be used in a role, in which case all
99 of that role's attributes will be undef-tolerant.
103 I often found myself in this quandry:
115 my $foo = ... # get the param from something
117 my $class = My:Class->new(foo => $foo, bar => 123);
119 What if foo is undefined? I didn't want to change my attribute to be
120 Maybe[Str] and I still want my predicate (C<has_foo>) to work. The only
124 $class = My:Class->new(foo => $foo, bar => 123);
126 $class = My:Class->new(bar => 123);
129 Or some type of codemulch using ternary conditionals. This module allows you
130 to make your attributes more tolerant of undef so that you can keep the first
131 example: have your cake and eat it too!
135 See L<MooseX::UndefTolerant::Attribute>.
139 This extension does not currently work in immutable classes when applying the
140 trait to some (but not all) attributes in the class. This is because the
141 inlined constructor initialization code currently lives in
142 L<Moose::Meta::Method::Constructor>, not L<Moose::Meta::Attribute>. The good
143 news is that this is expected to be changing shortly.
145 =head1 ACKNOWLEDGEMENTS
147 Many thanks to the crew in #moose who talked me through this module:
149 Hans Dieter Pearcey (confound)
155 Dylan Hardison (dylan)
157 Jay Shirley (jshirley)