1 package MooseX::StrictConstructor;
6 our $VERSION = '0.06_01';
7 $VERSION = eval $VERSION;
12 use Moose::Util::MetaRole;
13 use MooseX::StrictConstructor::Role::Object;
14 use MooseX::StrictConstructor::Role::Meta::Method::Constructor;
16 Moose::Exporter->setup_import_methods( also => 'Moose' );
25 my $caller = $p{for_class};
27 Moose::Util::MetaRole::apply_metaclass_roles
28 ( for_class => $caller,
29 constructor_class_roles =>
30 ['MooseX::StrictConstructor::Role::Meta::Method::Constructor'],
33 Moose::Util::MetaRole::apply_base_class_roles
34 ( for_class => $caller,
36 [ 'MooseX::StrictConstructor::Role::Object' ],
39 return $caller->meta();
50 MooseX::StrictConstructor - Make your object constructors blow up on unknown attributes
56 use MooseX::StrictConstructor; # instead of use Moose
62 # this blows up because color is not a known attribute
63 My::Class->new( size => 5, color => 'blue' );
67 Using this class to load Moose instead of just loading using Moose
68 itself makes your constructors "strict". If your constructor is called
69 with an attribute init argument that your class does not declare, then
70 it calls "Carp::confess()". This is a great way to catch small typos.
72 =head2 Subverting Strictness
74 You may find yourself wanting to have your constructor accept a
75 parameter which does not correspond to an attribute.
77 In that case, you'll probably also be writing a C<BUILD()> or
78 C<BUILDARGS()> method to deal with that parameter. In a C<BUILDARGS()>
79 method, you can simply make sure that this parameter is not included
80 in the hash reference you return. Otherwise, in a C<BUILD()> method,
81 you can delete it from the hash reference of parameters.
87 if ( delete $params->{do_something} ) {
94 Dave Rolsky, C<< <autarch@urth.org> >>
98 Please report any bugs or feature requests to
99 C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web
100 interface at L<http://rt.cpan.org>. I will be notified, and then
101 you'll automatically be notified of progress on your bug as I make
104 =head1 COPYRIGHT & LICENSE
106 Copyright 2007-2008 Dave Rolsky, All Rights Reserved.
108 This program is free software; you can redistribute it and/or modify
109 it under the same terms as Perl itself.