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