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