1 package MooseX::StrictConstructor;
8 use Moose::Util::MetaRole;
9 use MooseX::StrictConstructor::Role::Object;
12 my %class_meta = ( class => ['MooseX::StrictConstructor::Trait::Class'] );
15 if ( $Moose::VERSION < 1.9900 ) {
16 require MooseX::StrictConstructor::Trait::Method::Constructor;
17 $class_meta{constructor}
18 = ['MooseX::StrictConstructor::Trait::Method::Constructor'];
21 Moose::Exporter->setup_import_methods(
22 class_metaroles => \%class_meta,
23 base_class_roles => ['MooseX::StrictConstructor::Role::Object'],
29 # ABSTRACT: Make your object constructors blow up on unknown attributes
40 use MooseX::StrictConstructor;
46 # this blows up because color is not a known attribute
47 My::Class->new( size => 5, color => 'blue' );
51 Simply loading this module makes your constructors "strict". If your
52 constructor is called with an attribute init argument that your class
53 does not declare, then it calls C<Moose->throw_error()>. This is a great way
56 =head2 Subverting Strictness
58 You may find yourself wanting to have your constructor accept a
59 parameter which does not correspond to an attribute.
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.
71 if ( delete $params->{do_something} ) {
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