A working first version.
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor.pm
CommitLineData
32726d88 1package MooseX::StrictConstructor;
2
3use strict;
4use warnings;
5
6our $VERSION = '0.01';
7
8use Moose;
9use MooseX::Object::StrictConstructor;
10
11
12sub import
13{
14 my $caller = caller();
15
16 return if $caller eq 'main';
17
18 Moose::init_meta( $caller, 'MooseX::Object::StrictConstructor', 'Moose::Meta::Class' );
19
20 Moose->import( { into => $caller } );
21
22 return;
23}
24
25
26
271;
28
29__END__
30
31=pod
32
33=head1 NAME
34
2ffa7b60 35MooseX::StrictConstructor - Make your object constructors blow up on unknown attributes
32726d88 36
37=head1 SYNOPSIS
38
2ffa7b60 39 package My::Class;
32726d88 40
2ffa7b60 41 use MooseX::StrictConstructor; # instead of use Moose
32726d88 42
2ffa7b60 43 has 'size' => ...;
32726d88 44
2ffa7b60 45 # then later ...
46
47 # this blows up because color is not a known attribute
48 My::Class->new( size => 5, color => 'blue' );
32726d88 49
50=head1 DESCRIPTION
51
2ffa7b60 52Using this class to load Moose instead of just loading using Moose
53itself makes your constructors "strict". If your constructor is called
54with an attribute that your class does not declare, then it calls
55"Carp::confess()". This is a great way to catch small typos.
56
57=head2 Subverting Strictness
58
59You may find yourself wanting to accept a parameter to the constructor
60that is not the name of an attribute.
61
62In that case, you'll probably be writing a C<BUILD()> method to deal
63with it. Your C<BUILD()> method will receive two parameters, the new
64object, and a hash reference of parameters passed to the constructor.
65
66If you delete keys from this hash reference, then they will not be
67seen when this class does its checking.
68
69 sub BUILD {
70 my $self = shift;
71 my $params = shift;
32726d88 72
2ffa7b60 73 if ( delete $params->{do_something} ) {
74 ...
75 }
76 }
32726d88 77
78=head1 AUTHOR
79
80Dave Rolsky, C<< <autarch@urth.org> >>
81
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
90=head1 COPYRIGHT & LICENSE
91
92Copyright 2007 Dave Rolsky, All Rights Reserved.
93
94This program is free software; you can redistribute it and/or modify
95it under the same terms as Perl itself.
96
97=cut