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