1 package MooseX::StrictConstructor;
7 $VERSION = eval $VERSION;
12 use Moose::Util::MetaRole;
13 use MooseX::StrictConstructor::Role::Object;
14 use MooseX::StrictConstructor::Role::Meta::Method::Constructor;
17 Moose::Exporter->setup_import_methods();
26 my $caller = $p{for_class};
28 Moose::Util::MetaRole::apply_metaclass_roles
29 ( for_class => $caller,
30 constructor_class_roles =>
31 ['MooseX::StrictConstructor::Role::Meta::Method::Constructor'],
34 Moose::Util::MetaRole::apply_base_class_roles
35 ( for_class => $caller,
37 [ 'MooseX::StrictConstructor::Role::Object' ],
40 return $caller->meta();
51 MooseX::StrictConstructor - Make your object constructors blow up on unknown attributes
58 use MooseX::StrictConstructor;
64 # this blows up because color is not a known attribute
65 My::Class->new( size => 5, color => 'blue' );
69 Simply loading this module makes your constructors "strict". If your
70 constructor is called with an attribute init argument that your class
71 does not declare, then it calls "Carp::confess()". This is a great way
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 Dave Rolsky, C<< <autarch@urth.org> >>
100 Please report any bugs or feature requests to
101 C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web
102 interface at L<http://rt.cpan.org>. I will be notified, and then
103 you'll automatically be notified of progress on your bug as I make
106 =head1 COPYRIGHT & LICENSE
108 Copyright 2007-2008 Dave Rolsky, All Rights Reserved.
110 This program is free software; you can redistribute it and/or modify
111 it under the same terms as Perl itself.