1 package MooseX::UndefTolerant;
6 use MooseX::UndefTolerant::Attribute;
10 Moose::Exporter->setup_import_methods(
11 attribute_metaclass_roles => [ 'MooseX::UndefTolerant::Attribute' ]
20 MooseX::UndefTolerant - Make your attribute(s) tolerant to undef intitialization
27 use MooseX::UndefTolerant;
32 predicate => 'has_name'
35 # Meanwhile, under the city...
38 my $class = My::Class->new(name => undef);
39 $class->has_name # False!
41 Or, if you only want one attribute to have this behaviour:
46 use MooseX::UndefTolerant::Attribute;
49 traits => [ qw(MooseX::UndefTolerant::Attribute)],
52 predicate => 'has_bar'
57 Loading this module in your L<Moose> class makes initialization of your
58 attributes tolerant of undef. If you specify the value of undef to any of
59 the attributes they will not be initialized. Effectively behaving as if you
60 had not provided a value at all.
64 I often found myself in this quandry:
76 my $foo = ... # get the param from something
78 my $class = My:Class->new(foo => $foo, bar => 123);
80 What if foo is undefined? I didn't want to change my attribute to be
81 Maybe[Str] and I still want my predicate (C<has_foo>) to work. The only
85 $class = My:CLass->new(foo => $foo, bar => 123);
87 $class = My:CLass->new(bar => 123);
90 Or some type of codemulch using ternarys. This module allows you to make
91 your attributes more tolerant of undef so that you can keep the first
92 example: have your cake and eat it too!
98 Cory G Watson, C<< <gphat at cpan.org> >>
100 =head1 ACKNOWLEDGEMENTS
102 Many thanks to the crew in #moose who talked me through this module:
104 Hans Dieter Pearcey (confound)
110 Dylan Hardison (dylan)
112 Jay Shirley (jshirley)
116 =head1 COPYRIGHT & LICENSE
118 Copyright 2009 Cory G Watson.
120 This program is free software; you can redistribute it and/or modify it
121 under the terms of either: the GNU General Public License as published
122 by the Free Software Foundation; or the Artistic License.
124 See http://dev.perl.org/licenses/ for more information.