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