aa81a45d191336c7b2c2637caee31f2b25715652
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor.pm
1 package MooseX::StrictConstructor;
2
3 use strict;
4 use warnings;
5
6 use Moose 0.94 ();
7 use Moose::Exporter;
8 use Moose::Util::MetaRole;
9
10 use MooseX::StrictConstructor::Trait::Class;
11 use MooseX::StrictConstructor::Trait::Method::Constructor;;
12
13 my %metaroles = (
14     class => ['MooseX::StrictConstructor::Trait::Class'],
15 );
16
17 $metaroles{constructor}
18     = ['MooseX::StrictConstructor::Trait::Method::Constructor']
19     if $Moose::VERSION >= 1.9900;
20
21 Moose::Exporter->setup_import_methods( class_metaroles => \%metaroles );
22
23 1;
24
25 # ABSTRACT: Make your object constructors blow up on unknown attributes
26
27 __END__
28
29 =pod
30
31 =head1 SYNOPSIS
32
33     package My::Class;
34
35     use Moose;
36     use MooseX::StrictConstructor;
37
38     has 'size' => ...;
39
40     # then later ...
41
42     # this blows up because color is not a known attribute
43     My::Class->new( size => 5, color => 'blue' );
44
45 =head1 DESCRIPTION
46
47 Simply loading this module makes your constructors "strict". If your
48 constructor is called with an attribute init argument that your class
49 does not declare, then it calls C<Moose->throw_error()>. This is a great way
50 to catch small typos.
51
52 =head2 Subverting Strictness
53
54 You may find yourself wanting to have your constructor accept a
55 parameter which does not correspond to an attribute.
56
57 In that case, you'll probably also be writing a C<BUILD()> or
58 C<BUILDARGS()> method to deal with that parameter. In a C<BUILDARGS()>
59 method, you can simply make sure that this parameter is not included
60 in the hash reference you return. Otherwise, in a C<BUILD()> method,
61 you can delete it from the hash reference of parameters.
62
63   sub BUILD {
64       my $self   = shift;
65       my $params = shift;
66
67       if ( delete $params->{do_something} ) {
68           ...
69       }
70   }
71
72 =head1 BUGS
73
74 Please report any bugs or feature requests to
75 C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web
76 interface at L<http://rt.cpan.org>.  I will be notified, and then
77 you'll automatically be notified of progress on your bug as I make
78 changes.
79
80 =cut