1 package MooseX::StrictConstructor;
8 use Moose::Util::MetaRole;
9 use MooseX::StrictConstructor::Role::Object;
15 if ( $Moose::VERSION < 1.9900 ) {
16 require MooseX::StrictConstructor::Trait::Method::Constructor;
19 'MooseX::StrictConstructor::Trait::Method::Constructor']
23 require MooseX::StrictConstructor::Trait::Class;
25 = ( class => ['MooseX::StrictConstructor::Trait::Class'] );
28 role => ['MooseX::StrictConstructor::Trait::Role'],
29 application_to_class => ['MooseX::StrictConstructor::Trait::ApplicationToClass'],
30 application_to_role =>['MooseX::StrictConstructor::Trait::ApplicationToRole'],
34 Moose::Exporter->setup_import_methods(
35 class_metaroles => \%class_meta,
36 role_metaroles => \%role_meta,
37 base_class_roles => ['MooseX::StrictConstructor::Role::Object'],
43 # ABSTRACT: Make your object constructors blow up on unknown attributes
54 use MooseX::StrictConstructor;
60 # this blows up because color is not a known attribute
61 My::Class->new( size => 5, color => 'blue' );
65 Simply loading this module makes your constructors "strict". If your
66 constructor is called with an attribute init argument that your class
67 does not declare, then it calls C<Moose->throw_error()>. This is a great way
70 As of Moose 1.9900, this module can also be used in a role, in which case the
71 constructor of the consuming class will become strict.
74 =head2 Subverting Strictness
76 You may find yourself wanting to have your constructor accept a
77 parameter which does not correspond to an attribute.
79 In that case, you'll probably also be writing a C<BUILD()> or
80 C<BUILDARGS()> method to deal with that parameter. In a C<BUILDARGS()>
81 method, you can simply make sure that this parameter is not included
82 in the hash reference you return. Otherwise, in a C<BUILD()> method,
83 you can delete it from the hash reference of parameters.
89 if ( delete $params->{do_something} ) {
96 Please report any bugs or feature requests to
97 C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web
98 interface at L<http://rt.cpan.org>. I will be notified, and then
99 you'll automatically be notified of progress on your bug as I make