1MooseX::StrictConstructUosre(r3)Contributed Perl DocumenMtoaotsieoXn::StrictConstructor(3)
2
3
4
6 MooseX::StrictConstructor - Make your object constructors blow up on
7 unknown attributes
8
10 version 0.11
11
13 package My::Class;
14
15 use Moose;
16 use MooseX::StrictConstructor;
17
18 has 'size' => ...;
19
20 # then later ...
21
22 # this blows up because color is not a known attribute
23 My::Class->new( size => 5, color => 'blue' );
24
26 Simply loading this module makes your constructors "strict". If your
27 constructor is called with an attribute init argument that your class
28 does not declare, then it calls "Carp::confess()". This is a great way
29 to catch small typos.
30
31 Subverting Strictness
32 You may find yourself wanting to have your constructor accept a
33 parameter which does not correspond to an attribute.
34
35 In that case, you'll probably also be writing a "BUILD()" or
36 "BUILDARGS()" method to deal with that parameter. In a "BUILDARGS()"
37 method, you can simply make sure that this parameter is not included in
38 the hash reference you return. Otherwise, in a "BUILD()" method, you
39 can delete it from the hash reference of parameters.
40
41 sub BUILD {
42 my $self = shift;
43 my $params = shift;
44
45 if ( delete $params->{do_something} ) {
46 ...
47 }
48 }
49
51 Please report any bugs or feature requests to
52 "bug-moosex-strictconstructor@rt.cpan.org", or through the web
53 interface at <http://rt.cpan.org>. I will be notified, and then you'll
54 automatically be notified of progress on your bug as I make changes.
55
57 Dave Rolsky <autarch@urth.org>
58
60 This software is Copyright (c) 2010 by Dave Rolsky.
61
62 This is free software, licensed under:
63
64 The Artistic License 2.0
65
66
67
68perl v5.12.2 2010-09-09 MooseX::StrictConstructor(3)