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