1 package MooseX::StrictConstructor;
9 use MooseX::Object::StrictConstructor;
14 my $caller = caller();
16 return if $caller eq 'main';
18 Moose::init_meta( $caller, 'MooseX::Object::StrictConstructor', 'Moose::Meta::Class' );
20 Moose->import( { into => $caller } );
35 MooseX::StrictConstructor - Make your object constructors blow up on unknown attributes
41 use MooseX::StrictConstructor; # instead of use Moose
47 # this blows up because color is not a known attribute
48 My::Class->new( size => 5, color => 'blue' );
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.
57 =head2 Subverting Strictness
59 You may find yourself wanting to accept a parameter to the constructor
60 that is not the name of an attribute.
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.
66 If you delete keys from this hash reference, then they will not be
67 seen when this class does its checking.
73 if ( delete $params->{do_something} ) {
80 Dave Rolsky, C<< <autarch@urth.org> >>
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
90 =head1 COPYRIGHT & LICENSE
92 Copyright 2007 Dave Rolsky, All Rights Reserved.
94 This program is free software; you can redistribute it and/or modify
95 it under the same terms as Perl itself.