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