Remove extra whitespace
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor.pm
CommitLineData
32726d88 1package MooseX::StrictConstructor;
2
3use strict;
4use warnings;
5
2c33a849 6use Moose 0.94 ();
0cdff431 7use Moose::Exporter;
fbfaa61f 8use Moose::Util::MetaRole;
7815dbf4 9
10{
0dc0aea2 11 my %class_meta = ( class => ['MooseX::StrictConstructor::Trait::Class'] );
12
7815dbf4 13 if ( $Moose::VERSION < 1.9900 ) {
1a4f7732 14 require MooseX::StrictConstructor::Trait::Method::Constructor;
0dc0aea2 15 $class_meta{constructor}
16 = ['MooseX::StrictConstructor::Trait::Method::Constructor'];
7815dbf4 17 }
18
19 Moose::Exporter->setup_import_methods(
709eccb9 20 class_metaroles => \%class_meta,
7815dbf4 21 );
22}
32726d88 23
32726d88 241;
25
5b66b6d4 26# ABSTRACT: Make your object constructors blow up on unknown attributes
27
32726d88 28__END__
29
30=pod
31
32726d88 32=head1 SYNOPSIS
33
2ffa7b60 34 package My::Class;
32726d88 35
f2f2a5dc 36 use Moose;
37 use MooseX::StrictConstructor;
32726d88 38
2ffa7b60 39 has 'size' => ...;
32726d88 40
2ffa7b60 41 # then later ...
42
43 # this blows up because color is not a known attribute
44 My::Class->new( size => 5, color => 'blue' );
32726d88 45
46=head1 DESCRIPTION
47
f2f2a5dc 48Simply loading this module makes your constructors "strict". If your
49constructor is called with an attribute init argument that your class
714128ef 50does not declare, then it calls C<Moose->throw_error()>. This is a great way
f2f2a5dc 51to catch small typos.
2ffa7b60 52
53=head2 Subverting Strictness
54
fbfaa61f 55You may find yourself wanting to have your constructor accept a
56parameter which does not correspond to an attribute.
2ffa7b60 57
fbfaa61f 58In that case, you'll probably also be writing a C<BUILD()> or
59C<BUILDARGS()> method to deal with that parameter. In a C<BUILDARGS()>
60method, you can simply make sure that this parameter is not included
61in the hash reference you return. Otherwise, in a C<BUILD()> method,
62you can delete it from the hash reference of parameters.
2ffa7b60 63
64 sub BUILD {
65 my $self = shift;
66 my $params = shift;
32726d88 67
2ffa7b60 68 if ( delete $params->{do_something} ) {
69 ...
70 }
71 }
32726d88 72
32726d88 73=head1 BUGS
74
2ffa7b60 75Please report any bugs or feature requests to
76C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web
77interface at L<http://rt.cpan.org>. I will be notified, and then
78you'll automatically be notified of progress on your bug as I make
79changes.
32726d88 80
32726d88 81=cut