determine prereqs automatically
[gitmo/MooseX-UndefTolerant.git] / README
CommitLineData
033dc180 1NAME
2 MooseX::UndefTolerant - Make your attribute(s) tolerant to undef
3 initialization
5447ee45 4
033dc180 5SYNOPSIS
6 package My::Class;
5447ee45 7
033dc180 8 use Moose;
9 use MooseX::UndefTolerant;
5447ee45 10
033dc180 11 has 'name' => (
12 is => 'ro',
13 isa => 'Str',
14 predicate => 'has_name'
15 );
5447ee45 16
033dc180 17 # Meanwhile, under the city...
5447ee45 18
033dc180 19 # Doesn't explode
20 my $class = My::Class->new(name => undef);
21 $class->has_name # False!
5447ee45 22
033dc180 23 Or, if you only want one attribute to have this behaviour:
5447ee45 24
033dc180 25 package My:Class;
26 use Moose;
5447ee45 27
033dc180 28 use MooseX::UndefTolerant::Attribute;
5447ee45 29
033dc180 30 has 'bar' => (
31 traits => [ qw(MooseX::UndefTolerant::Attribute)],
32 is => 'ro',
33 isa => 'Num',
34 predicate => 'has_bar'
35 );
5447ee45 36
033dc180 37DESCRIPTION
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.
5447ee45 42
033dc180 43MOTIVATION
44 I often found myself in this quandry:
5447ee45 45
033dc180 46 package My:Class;
47 use Moose;
5447ee45 48
033dc180 49 has 'foo' => (
50 is => 'ro',
51 isa => 'Str',
52 );
5447ee45 53
033dc180 54 # ... then
5447ee45 55
033dc180 56 my $foo = ... # get the param from something
5447ee45 57
033dc180 58 my $class = My:Class->new(foo => $foo, bar => 123);
5447ee45 59
033dc180 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:
5447ee45 63
033dc180 64 if(defined($foo)) {
65 $class = My:Class->new(foo => $foo, bar => 123);
66 } else {
67 $class = My:Class->new(bar => 123);
68 }
5447ee45 69
033dc180 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
74PER ATTRIBUTE
75 See MooseX::UndefTolerant::Attribute.
76
77CAVEATS
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
84ACKNOWLEDGEMENTS
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
99AUTHOR
100 Cory G Watson <gphat at cpan.org>
101
102COPYRIGHT 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.
5447ee45 107