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