1 package MooseX::StrictConstructor;
6 our $VERSION = '0.06_01';
7 eval $VERSION = $VERSION;
12 use MooseX::StrictConstructor::Role::Object;
13 use MooseX::StrictConstructor::Role::Metaclass;
15 Moose::Exporter->setup_import_methods( also => 'Moose' );
24 my $caller = $p{for_class};
26 my $metameta = $caller->meta()->meta();
27 unless ( $metameta->can('does_role')
28 && $metameta->does_role( 'MooseX::StrictConstructor::Role::Metaclass' ) )
31 Moose::Meta::Class->create_anon_class
32 ( superclasses => [ ref $caller->meta() ],
33 roles => [ 'MooseX::StrictConstructor::Role::Metaclass' ],
37 Class::MOP::remove_metaclass_by_name($caller);
39 $new_meta->name()->initialize($caller);
42 unless ( $caller->meta()->does_role('MooseX::StrictConstructor::Role::Object') )
45 Moose::Meta::Class->create_anon_class
46 ( superclasses => [ $caller->meta()->superclasses() ],
47 roles => [ 'MooseX::StrictConstructor::Role::Object' ],
51 $caller->meta()->superclasses( $new_base->name() );
54 return $caller->meta();
65 MooseX::StrictConstructor - Make your object constructors blow up on unknown attributes
71 use MooseX::StrictConstructor; # instead of use Moose
77 # this blows up because color is not a known attribute
78 My::Class->new( size => 5, color => 'blue' );
82 Using this class to load Moose instead of just loading using Moose
83 itself makes your constructors "strict". If your constructor is called
84 with an attribute init argument that your class does not declare, then
85 it calls "Carp::confess()". This is a great way to catch small typos.
87 =head2 Subverting Strictness
89 You may find yourself wanting to accept a parameter to the constructor
90 that is not the name of an attribute.
92 In that case, you'll probably be writing a C<BUILD()> method to deal
93 with it. Your C<BUILD()> method will receive two parameters, the new
94 object, and a hash reference of parameters passed to the constructor.
96 If you delete keys from this hash reference, then they will not be
97 seen when this class does its checking.
103 if ( delete $params->{do_something} ) {
110 Using this class replaces the default Moose meta class,
111 C<Moose::Meta::Class>, with its own,
112 C<MooseX::StrictConstructor::Meta::Class>. If you have your own meta
113 class, this distro will probably not work for you.
117 Dave Rolsky, C<< <autarch@urth.org> >>
121 Please report any bugs or feature requests to
122 C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web
123 interface at L<http://rt.cpan.org>. I will be notified, and then
124 you'll automatically be notified of progress on your bug as I make
127 =head1 COPYRIGHT & LICENSE
129 Copyright 2007 Dave Rolsky, All Rights Reserved.
131 This program is free software; you can redistribute it and/or modify
132 it under the same terms as Perl itself.