determine prereqs automatically
[gitmo/MooseX-UndefTolerant.git] / README
1 NAME
2     MooseX::UndefTolerant - Make your attribute(s) tolerant to undef
3     initialization
4
5 SYNOPSIS
6       package My::Class;
7
8       use Moose;
9       use MooseX::UndefTolerant;
10
11       has 'name' => (
12         is => 'ro',
13         isa => 'Str',
14         predicate => 'has_name'
15       );
16
17       # Meanwhile, under the city...
18
19       # Doesn't explode
20       my $class = My::Class->new(name => undef);
21       $class->has_name # False!
22
23     Or, if you only want one attribute to have this behaviour:
24
25       package My:Class;
26       use Moose;
27
28       use MooseX::UndefTolerant::Attribute;
29
30       has 'bar' => (
31           traits => [ qw(MooseX::UndefTolerant::Attribute)],
32           is => 'ro',
33           isa => 'Num',
34           predicate => 'has_bar'
35       );
36
37 DESCRIPTION
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.
42
43 MOTIVATION
44     I often found myself in this quandry:
45
46       package My:Class;
47       use Moose;
48
49       has 'foo' => (
50         is => 'ro',
51         isa => 'Str',
52       );
53
54       # ... then
55
56       my $foo = ... # get the param from something
57
58       my $class = My:Class->new(foo => $foo, bar => 123);
59
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
62     real solution was:
63
64       if(defined($foo)) {
65         $class = My:Class->new(foo => $foo, bar => 123);
66       } else {
67         $class = My:Class->new(bar => 123);
68       }
69
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!
73
74 PER ATTRIBUTE
75     See MooseX::UndefTolerant::Attribute.
76
77 CAVEATS
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.
83
84 ACKNOWLEDGEMENTS
85     Many thanks to the crew in #moose who talked me through this module:
86
87     Hans Dieter Pearcey (confound)
88
89     Jesse Luehrs (doy)
90
91     Tomas Doran (t0m)
92
93     Dylan Hardison (dylan)
94
95     Jay Shirley (jshirley)
96
97     Mike Eldridge (diz)
98
99 AUTHOR
100     Cory G Watson <gphat at cpan.org>
101
102 COPYRIGHT AND LICENSE
103     This software is copyright (c) 2011 by Cory G Watson.
104
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.
107