2 MooseX::UndefTolerant - Make your attribute(s) tolerant to undef
9 use MooseX::UndefTolerant;
14 predicate => 'has_name'
17 # Meanwhile, under the city...
20 my $class = My::Class->new(name => undef);
21 $class->has_name # False!
23 Or, if you only want one attribute to have this behaviour:
28 use MooseX::UndefTolerant::Attribute;
31 traits => [ qw(MooseX::UndefTolerant::Attribute)],
34 predicate => 'has_bar'
38 Loading this module in your Moose class makes initialization of your
39 attributes tolerant of undef. If you specify the value of undef to any
40 of the attributes they will not be initialized, effectively behaving as
41 if you had not provided a value at all.
44 I often found myself in this quandry:
56 my $foo = ... # get the param from something
58 my $class = My:Class->new(foo => $foo, bar => 123);
60 What if foo is undefined? I didn't want to change my attribute to be
61 Maybe[Str] and I still want my predicate ("has_foo") to work. The only
65 $class = My:Class->new(foo => $foo, bar => 123);
67 $class = My:Class->new(bar => 123);
70 Or some type of codemulch using ternarys. This module allows you to make
71 your attributes more tolerant of undef so that you can keep the first
72 example: have your cake and eat it too!
75 See MooseX::UndefTolerant::Attribute.
78 This extension does not currently work in immutable classes when
79 applying the trait to some (but not all) attributes in the class. This
80 is because the inlined constructor initialization code currently lives
81 in Moose::Meta::Method::Constructor, not Moose::Meta::Attribute. The
82 good news is that this is expected to be changing shortly.
85 Many thanks to the crew in #moose who talked me through this module:
87 Hans Dieter Pearcey (confound)
93 Dylan Hardison (dylan)
95 Jay Shirley (jshirley)
100 Cory G Watson <gphat at cpan.org>
102 COPYRIGHT AND LICENSE
103 This software is copyright (c) 2011 by Cory G Watson.
105 This is free software; you can redistribute it and/or modify it under
106 the same terms as the Perl 5 programming language system itself.