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