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