look at init_arg, not attribute name
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor.pm
CommitLineData
32726d88 1package MooseX::StrictConstructor;
2
3use strict;
4use warnings;
5
a83dec43 6our $VERSION = '0.04';
32726d88 7
8use Moose;
9use MooseX::Object::StrictConstructor;
10
11
12sub import
13{
14 my $caller = caller();
15
16 return if $caller eq 'main';
17
c001451a 18 Moose::init_meta( $caller,
19 'MooseX::Object::StrictConstructor',
20 'MooseX::StrictConstructor::Meta::Class',
21 );
32726d88 22
23 Moose->import( { into => $caller } );
24
25 return;
26}
27
28
29
301;
31
32__END__
33
34=pod
35
36=head1 NAME
37
2ffa7b60 38MooseX::StrictConstructor - Make your object constructors blow up on unknown attributes
32726d88 39
40=head1 SYNOPSIS
41
2ffa7b60 42 package My::Class;
32726d88 43
2ffa7b60 44 use MooseX::StrictConstructor; # instead of use Moose
32726d88 45
2ffa7b60 46 has 'size' => ...;
32726d88 47
2ffa7b60 48 # then later ...
49
50 # this blows up because color is not a known attribute
51 My::Class->new( size => 5, color => 'blue' );
32726d88 52
53=head1 DESCRIPTION
54
2ffa7b60 55Using this class to load Moose instead of just loading using Moose
56itself makes your constructors "strict". If your constructor is called
a83dec43 57with an attribute init argument that your class does not declare, then
58it calls "Carp::confess()". This is a great way to catch small typos.
2ffa7b60 59
60=head2 Subverting Strictness
61
62You may find yourself wanting to accept a parameter to the constructor
63that is not the name of an attribute.
64
65In that case, you'll probably be writing a C<BUILD()> method to deal
66with it. Your C<BUILD()> method will receive two parameters, the new
67object, and a hash reference of parameters passed to the constructor.
68
69If you delete keys from this hash reference, then they will not be
70seen when this class does its checking.
71
72 sub BUILD {
73 my $self = shift;
74 my $params = shift;
32726d88 75
2ffa7b60 76 if ( delete $params->{do_something} ) {
77 ...
78 }
79 }
32726d88 80
c001451a 81=head2 Caveats
82
83Using this class replaces the default Moose meta class,
84C<Moose::Meta::Class>, with its own,
85C<MooseX::StrictConstructor::Meta::Class>. If you have your own meta
86class, this distro will probably not work for you.
87
32726d88 88=head1 AUTHOR
89
90Dave Rolsky, C<< <autarch@urth.org> >>
91
92=head1 BUGS
93
2ffa7b60 94Please report any bugs or feature requests to
95C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web
96interface at L<http://rt.cpan.org>. I will be notified, and then
97you'll automatically be notified of progress on your bug as I make
98changes.
32726d88 99
100=head1 COPYRIGHT & LICENSE
101
102Copyright 2007 Dave Rolsky, All Rights Reserved.
103
104This program is free software; you can redistribute it and/or modify
105it under the same terms as Perl itself.
106
107=cut