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