1 package MooseX::UndefTolerant;
6 use MooseX::UndefTolerant::Attribute;
7 use MooseX::UndefTolerant::Class;
8 use MooseX::UndefTolerant::Constructor;
10 our $VERSION = '0.08';
12 my %metaroles = ( attribute => [ 'MooseX::UndefTolerant::Attribute' ] );
13 if ( $Moose::VERSION < 1.9900 ) {
14 $metaroles{constructor} = [ 'MooseX::UndefTolerant::Constructor' ];
17 $metaroles{class} = [ 'MooseX::UndefTolerant::Class' ];
21 Moose::Exporter->setup_import_methods(
22 class_metaroles => \%metaroles,
31 MooseX::UndefTolerant - Make your attribute(s) tolerant to undef initialization
38 use MooseX::UndefTolerant;
43 predicate => 'has_name'
46 # Meanwhile, under the city...
49 my $class = My::Class->new(name => undef);
50 $class->has_name # False!
52 Or, if you only want one attribute to have this behaviour:
57 use MooseX::UndefTolerant::Attribute;
60 traits => [ qw(MooseX::UndefTolerant::Attribute)],
63 predicate => 'has_bar'
68 Loading this module in your L<Moose> class makes initialization of your
69 attributes tolerant of undef. If you specify the value of undef to any of
70 the attributes they will not be initialized, effectively behaving as if you
71 had not provided a value at all.
75 I often found myself in this quandry:
87 my $foo = ... # get the param from something
89 my $class = My:Class->new(foo => $foo, bar => 123);
91 What if foo is undefined? I didn't want to change my attribute to be
92 Maybe[Str] and I still want my predicate (C<has_foo>) to work. The only
96 $class = My:Class->new(foo => $foo, bar => 123);
98 $class = My:Class->new(bar => 123);
101 Or some type of codemulch using ternarys. This module allows you to make
102 your attributes more tolerant of undef so that you can keep the first
103 example: have your cake and eat it too!
107 See L<MooseX::UndefTolerant::Attribute>.
111 This extension does not currently work in immutable classes when applying the
112 trait to some (but not all) attributes in the class. This is because the
113 inlined constructor initialization code currently lives in
114 L<Moose::Meta::Method::Constructor>, not L<Moose::Meta::Attribute>. The good
115 news is that this is expected to be changing shortly.
119 Cory G Watson, C<< <gphat at cpan.org> >>
121 =head1 ACKNOWLEDGEMENTS
123 Many thanks to the crew in #moose who talked me through this module:
125 Hans Dieter Pearcey (confound)
131 Dylan Hardison (dylan)
133 Jay Shirley (jshirley)
137 =head1 COPYRIGHT & LICENSE
139 Copyright 2009 Cory G Watson.
141 This program is free software; you can redistribute it and/or modify it
142 under the terms of either: the GNU General Public License as published
143 by the Free Software Foundation; or the Artistic License.
145 See http://dev.perl.org/licenses/ for more information.