1 package MooseX::StrictConstructor;
9 use MooseX::Object::StrictConstructor;
14 my $caller = caller();
16 return if $caller eq 'main';
18 Moose::init_meta( $caller,
19 'MooseX::Object::StrictConstructor',
20 'MooseX::StrictConstructor::Meta::Class',
23 Moose->import( { into => $caller } );
38 MooseX::StrictConstructor - Make your object constructors blow up on unknown attributes
44 use MooseX::StrictConstructor; # instead of use Moose
50 # this blows up because color is not a known attribute
51 My::Class->new( size => 5, color => 'blue' );
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 init argument that your class does not declare, then
58 it calls "Carp::confess()". This is a great way to catch small typos.
60 =head2 Subverting Strictness
62 You may find yourself wanting to accept a parameter to the constructor
63 that is not the name of an attribute.
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.
69 If you delete keys from this hash reference, then they will not be
70 seen when this class does its checking.
76 if ( delete $params->{do_something} ) {
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.
90 Dave Rolsky, C<< <autarch@urth.org> >>
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
100 =head1 COPYRIGHT & LICENSE
102 Copyright 2007 Dave Rolsky, All Rights Reserved.
104 This program is free software; you can redistribute it and/or modify
105 it under the same terms as Perl itself.