bump version to 0.09
[gitmo/MooseX-UndefTolerant.git] / lib / MooseX / UndefTolerant.pm
CommitLineData
5447ee45 1package MooseX::UndefTolerant;
5447ee45 2
3use Moose qw();
4use Moose::Exporter;
5447ee45 5
6use MooseX::UndefTolerant::Attribute;
d6ce838b 7use MooseX::UndefTolerant::Class;
2d1c57bd 8use MooseX::UndefTolerant::Constructor;
5447ee45 9
7e9c113d 10our $VERSION = '0.09';
5447ee45 11
d6ce838b 12my %metaroles = ( attribute => [ 'MooseX::UndefTolerant::Attribute' ] );
13if ( $Moose::VERSION < 1.9900 ) {
14 $metaroles{constructor} = [ 'MooseX::UndefTolerant::Constructor' ];
15}
16else {
17 $metaroles{class} = [ 'MooseX::UndefTolerant::Class' ];
18}
19
20
e92df8d4 21Moose::Exporter->setup_import_methods(
d6ce838b 22 class_metaroles => \%metaroles,
e92df8d4 23);
5447ee45 24
251;
26
27__END__
28
29=head1 NAME
30
d0abfb45 31MooseX::UndefTolerant - Make your attribute(s) tolerant to undef initialization
5447ee45 32
33=head1 SYNOPSIS
34
19258058 35 package My::Class;
5447ee45 36
19258058 37 use Moose;
38 use MooseX::UndefTolerant;
39
40 has 'name' => (
41 is => 'ro',
42 isa => 'Str',
43 predicate => 'has_name'
44 );
45
46 # Meanwhile, under the city...
47
48 # Doesn't explode
49 my $class = My::Class->new(name => undef);
50 $class->has_name # False!
51
52Or, if you only want one attribute to have this behaviour:
53
54 package My:Class;
55 use Moose;
56
57 use MooseX::UndefTolerant::Attribute;
58
59 has 'bar' => (
60 traits => [ qw(MooseX::UndefTolerant::Attribute)],
61 is => 'ro',
62 isa => 'Num',
63 predicate => 'has_bar'
64 );
65
66=head1 DESCRIPTION
67
68Loading this module in your L<Moose> class makes initialization of your
69attributes tolerant of undef. If you specify the value of undef to any of
94f9d198 70the attributes they will not be initialized, effectively behaving as if you
19258058 71had not provided a value at all.
72
73=head1 MOTIVATION
74
75I often found myself in this quandry:
76
77 package My:Class;
78 use Moose;
79
80 has 'foo' => (
81 is => 'ro',
82 isa => 'Str',
83 );
84
85 # ... then
86
87 my $foo = ... # get the param from something
88
89 my $class = My:Class->new(foo => $foo, bar => 123);
90
91What if foo is undefined? I didn't want to change my attribute to be
92Maybe[Str] and I still want my predicate (C<has_foo>) to work. The only
93real solution was:
94
95 if(defined($foo)) {
1cbb963b 96 $class = My:Class->new(foo => $foo, bar => 123);
19258058 97 } else {
1cbb963b 98 $class = My:Class->new(bar => 123);
19258058 99 }
100
101Or some type of codemulch using ternarys. This module allows you to make
102your attributes more tolerant of undef so that you can keep the first
103example: have your cake and eat it too!
104
105=head1 PER ATTRIBUTE
5447ee45 106
1cbb963b 107See L<MooseX::UndefTolerant::Attribute>.
108
94f9d198 109=head1 CAVEATS
110
111This extension does not currently work in immutable classes when applying the
112trait to some (but not all) attributes in the class. This is because the
113inlined constructor initialization code currently lives in
114L<Moose::Meta::Method::Constructor>, not L<Moose::Meta::Attribute>. The good
115news is that this is expected to be changing shortly.
116
5447ee45 117=head1 AUTHOR
118
119Cory G Watson, C<< <gphat at cpan.org> >>
120
121=head1 ACKNOWLEDGEMENTS
122
19258058 123Many thanks to the crew in #moose who talked me through this module:
124
5447ee45 125Hans Dieter Pearcey (confound)
126
127Jesse Luehrs (doy)
128
129Tomas Doran (t0m)
130
131Dylan Hardison (dylan)
132
133Jay Shirley (jshirley)
134
135Mike Eldridge (diz)
136
137=head1 COPYRIGHT & LICENSE
138
139Copyright 2009 Cory G Watson.
140
141This program is free software; you can redistribute it and/or modify it
142under the terms of either: the GNU General Public License as published
143by the Free Software Foundation; or the Artistic License.
144
145See http://dev.perl.org/licenses/ for more information.
146
5447ee45 147=cut