1 package MooseX::StrictConstructor;
11 use MooseX::StrictConstructor::Role::Object;
12 use MooseX::StrictConstructor::Role::Metaclass;
14 Moose::Exporter->setup_import_methods( also => 'Moose' );
23 my $caller = $p{for_class};
25 my $metameta = $caller->meta()->meta();
26 unless ( $metameta->can('does_role')
27 && $metameta->does_role( 'MooseX::StrictConstructor::Role::Metaclass' ) )
30 Moose::Meta::Class->create_anon_class
31 ( superclasses => [ ref $caller->meta() ],
32 roles => [ 'MooseX::StrictConstructor::Role::Metaclass' ],
36 Class::MOP::remove_metaclass_by_name($caller);
38 $new_meta->name()->initialize($caller);
41 unless ( $caller->meta()->does_role('MooseX::StrictConstructor::Role::Object') )
44 Moose::Meta::Class->create_anon_class
45 ( superclasses => [ $caller->meta()->superclasses() ],
46 roles => [ 'MooseX::StrictConstructor::Role::Object' ],
50 $caller->meta()->superclasses( $new_base->name() );
53 return $caller->meta();
64 MooseX::StrictConstructor - Make your object constructors blow up on unknown attributes
70 use MooseX::StrictConstructor; # instead of use Moose
76 # this blows up because color is not a known attribute
77 My::Class->new( size => 5, color => 'blue' );
81 Using this class to load Moose instead of just loading using Moose
82 itself makes your constructors "strict". If your constructor is called
83 with an attribute init argument that your class does not declare, then
84 it calls "Carp::confess()". This is a great way to catch small typos.
86 =head2 Subverting Strictness
88 You may find yourself wanting to accept a parameter to the constructor
89 that is not the name of an attribute.
91 In that case, you'll probably be writing a C<BUILD()> method to deal
92 with it. Your C<BUILD()> method will receive two parameters, the new
93 object, and a hash reference of parameters passed to the constructor.
95 If you delete keys from this hash reference, then they will not be
96 seen when this class does its checking.
102 if ( delete $params->{do_something} ) {
109 Using this class replaces the default Moose meta class,
110 C<Moose::Meta::Class>, with its own,
111 C<MooseX::StrictConstructor::Meta::Class>. If you have your own meta
112 class, this distro will probably not work for you.
116 Dave Rolsky, C<< <autarch@urth.org> >>
120 Please report any bugs or feature requests to
121 C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web
122 interface at L<http://rt.cpan.org>. I will be notified, and then
123 you'll automatically be notified of progress on your bug as I make
126 =head1 COPYRIGHT & LICENSE
128 Copyright 2007 Dave Rolsky, All Rights Reserved.
130 This program is free software; you can redistribute it and/or modify
131 it under the same terms as Perl itself.