1 package MooseX::UndefTolerant;
6 use MooseX::UndefTolerant::Attribute;
7 use MooseX::UndefTolerant::Class;
8 use MooseX::UndefTolerant::Constructor;
11 my %metaroles = ( attribute => [ 'MooseX::UndefTolerant::Attribute' ] );
12 if ( $Moose::VERSION < 1.9900 ) {
13 $metaroles{constructor} = [ 'MooseX::UndefTolerant::Constructor' ];
16 $metaroles{class} = [ 'MooseX::UndefTolerant::Class' ];
20 Moose::Exporter->setup_import_methods(
21 class_metaroles => \%metaroles,
26 # ABSTRACT: Make your attribute(s) tolerant to undef initialization
35 use MooseX::UndefTolerant;
40 predicate => 'has_name'
43 # Meanwhile, under the city...
46 my $class = My::Class->new(name => undef);
47 $class->has_name # False!
49 Or, if you only want one attribute to have this behaviour:
54 use MooseX::UndefTolerant::Attribute;
57 traits => [ qw(MooseX::UndefTolerant::Attribute)],
60 predicate => 'has_bar'
65 Loading this module in your L<Moose> class makes initialization of your
66 attributes tolerant of undef. If you specify the value of undef to any of
67 the attributes they will not be initialized, effectively behaving as if you
68 had not provided a value at all.
72 I often found myself in this quandry:
84 my $foo = ... # get the param from something
86 my $class = My:Class->new(foo => $foo, bar => 123);
88 What if foo is undefined? I didn't want to change my attribute to be
89 Maybe[Str] and I still want my predicate (C<has_foo>) to work. The only
93 $class = My:Class->new(foo => $foo, bar => 123);
95 $class = My:Class->new(bar => 123);
98 Or some type of codemulch using ternarys. This module allows you to make
99 your attributes more tolerant of undef so that you can keep the first
100 example: have your cake and eat it too!
104 See L<MooseX::UndefTolerant::Attribute>.
108 This extension does not currently work in immutable classes when applying the
109 trait to some (but not all) attributes in the class. This is because the
110 inlined constructor initialization code currently lives in
111 L<Moose::Meta::Method::Constructor>, not L<Moose::Meta::Attribute>. The good
112 news is that this is expected to be changing shortly.
114 =head1 ACKNOWLEDGEMENTS
116 Many thanks to the crew in #moose who talked me through this module:
118 Hans Dieter Pearcey (confound)
124 Dylan Hardison (dylan)
126 Jay Shirley (jshirley)