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