Commit | Line | Data |
32726d88 |
1 | package MooseX::StrictConstructor; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
f65b0fb9 |
6 | our $VERSION = '0.03'; |
32726d88 |
7 | |
8 | use Moose; |
9 | use MooseX::Object::StrictConstructor; |
10 | |
11 | |
12 | sub import |
13 | { |
14 | my $caller = caller(); |
15 | |
16 | return if $caller eq 'main'; |
17 | |
c001451a |
18 | Moose::init_meta( $caller, |
19 | 'MooseX::Object::StrictConstructor', |
20 | 'MooseX::StrictConstructor::Meta::Class', |
21 | ); |
32726d88 |
22 | |
23 | Moose->import( { into => $caller } ); |
24 | |
25 | return; |
26 | } |
27 | |
28 | |
29 | |
30 | 1; |
31 | |
32 | __END__ |
33 | |
34 | =pod |
35 | |
36 | =head1 NAME |
37 | |
2ffa7b60 |
38 | MooseX::StrictConstructor - Make your object constructors blow up on unknown attributes |
32726d88 |
39 | |
40 | =head1 SYNOPSIS |
41 | |
2ffa7b60 |
42 | package My::Class; |
32726d88 |
43 | |
2ffa7b60 |
44 | use MooseX::StrictConstructor; # instead of use Moose |
32726d88 |
45 | |
2ffa7b60 |
46 | has 'size' => ...; |
32726d88 |
47 | |
2ffa7b60 |
48 | # then later ... |
49 | |
50 | # this blows up because color is not a known attribute |
51 | My::Class->new( size => 5, color => 'blue' ); |
32726d88 |
52 | |
53 | =head1 DESCRIPTION |
54 | |
2ffa7b60 |
55 | Using this class to load Moose instead of just loading using Moose |
56 | itself makes your constructors "strict". If your constructor is called |
57 | with an attribute that your class does not declare, then it calls |
58 | "Carp::confess()". This is a great way to catch small typos. |
59 | |
60 | =head2 Subverting Strictness |
61 | |
62 | You may find yourself wanting to accept a parameter to the constructor |
63 | that is not the name of an attribute. |
64 | |
65 | In that case, you'll probably be writing a C<BUILD()> method to deal |
66 | with it. Your C<BUILD()> method will receive two parameters, the new |
67 | object, and a hash reference of parameters passed to the constructor. |
68 | |
69 | If you delete keys from this hash reference, then they will not be |
70 | seen when this class does its checking. |
71 | |
72 | sub BUILD { |
73 | my $self = shift; |
74 | my $params = shift; |
32726d88 |
75 | |
2ffa7b60 |
76 | if ( delete $params->{do_something} ) { |
77 | ... |
78 | } |
79 | } |
32726d88 |
80 | |
c001451a |
81 | =head2 Caveats |
82 | |
83 | Using this class replaces the default Moose meta class, |
84 | C<Moose::Meta::Class>, with its own, |
85 | C<MooseX::StrictConstructor::Meta::Class>. If you have your own meta |
86 | class, this distro will probably not work for you. |
87 | |
32726d88 |
88 | =head1 AUTHOR |
89 | |
90 | Dave Rolsky, C<< <autarch@urth.org> >> |
91 | |
92 | =head1 BUGS |
93 | |
2ffa7b60 |
94 | Please report any bugs or feature requests to |
95 | C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web |
96 | interface at L<http://rt.cpan.org>. I will be notified, and then |
97 | you'll automatically be notified of progress on your bug as I make |
98 | changes. |
32726d88 |
99 | |
100 | =head1 COPYRIGHT & LICENSE |
101 | |
102 | Copyright 2007 Dave Rolsky, All Rights Reserved. |
103 | |
104 | This program is free software; you can redistribute it and/or modify |
105 | it under the same terms as Perl itself. |
106 | |
107 | =cut |