1 package MooseX::StrictConstructor;
8 use Moose::Util::MetaRole;
11 my %class_meta = ( class => ['MooseX::StrictConstructor::Trait::Class'] );
14 if ( $Moose::VERSION < 1.9900 ) {
15 require MooseX::StrictConstructor::Trait::Method::Constructor;
16 $class_meta{constructor}
17 = ['MooseX::StrictConstructor::Trait::Method::Constructor'];
20 Moose::Exporter->setup_import_methods(
21 class_metaroles => \%class_meta,
24 my $old_import = __PACKAGE__->can('import');
25 no warnings 'redefine';
28 Carp::croak "$_[0] can only be applied to Moose classes"
29 unless eval { $caller->meta->isa('Moose::Meta::Class') };
37 # ABSTRACT: Make your object constructors blow up on unknown attributes
48 use MooseX::StrictConstructor;
54 # this blows up because color is not a known attribute
55 My::Class->new( size => 5, color => 'blue' );
59 Simply loading this module makes your constructors "strict". If your
60 constructor is called with an attribute init argument that your class
61 does not declare, then it calls C<Moose->throw_error()>. This is a great way
64 =head2 Subverting Strictness
66 You may find yourself wanting to have your constructor accept a
67 parameter which does not correspond to an attribute.
69 In that case, you'll probably also be writing a C<BUILD()> or
70 C<BUILDARGS()> method to deal with that parameter. In a C<BUILDARGS()>
71 method, you can simply make sure that this parameter is not included
72 in the hash reference you return. Otherwise, in a C<BUILD()> method,
73 you can delete it from the hash reference of parameters.
79 if ( delete $params->{do_something} ) {
86 Please report any bugs or feature requests to
87 C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web
88 interface at L<http://rt.cpan.org>. I will be notified, and then
89 you'll automatically be notified of progress on your bug as I make