1MooX::StrictConstructorU(s3e)r Contributed Perl DocumentaMtoiooXn::StrictConstructor(3)
2
3
4
6 MooX::StrictConstructor - Make your Moo-based object constructors blow
7 up on unknown attributes.
8
10 version 0.010
11
13 package My::Class;
14
15 use Moo;
16 use MooX::StrictConstructor;
17
18 has 'size' => ( is => 'rw');
19
20 # then somewhere else, when constructing a new instance
21 # of My::Class ...
22
23 # this blows up because color is not a known attribute
24 My::Class->new( size => 5, color => 'blue' );
25
27 Simply loading this module makes your constructors "strict". If your
28 constructor is called with an attribute init argument that your class
29 does not declare, then it dies. This is a great way to catch small
30 typos.
31
32 STANDING ON THE SHOULDERS OF ...
33 Most of this package was lifted from MooX::InsideOut and most of the
34 Role that implements the strictness was lifted from
35 MooseX::StrictConstructor.
36
37 SUBVERTING STRICTNESS
38 MooseX::StrictConstructor documents two tricks for subverting
39 strictness and avoid having problematic arguments cause an exception:
40 handling them in BUILD or handle them in BUILDARGS.
41
42 In MooX::StrictConstructor you can use a BUILDARGS function to handle
43 them, e.g. this will allow you to pass in a parameter called "spy"
44 without raising an exception. Useful? Only you can tell.
45
46 sub BUILDARGS {
47 my ($self, %params) = @_;
48 my $spy delete $params{spy};
49 # do something useful with the spy param
50 return \%params;
51 }
52
53 Because "BUILD" methods are run after an object has been constructed
54 and this code runs before the object is constructed the "BUILD" trick
55 will not work.
56
58 Inheritance
59 A class that uses MooX::StrictConstructor but extends another class
60 that does not will not be handled properly. This code hooks into the
61 constructor as it is being strung up (literally) and that happens in
62 the parent class, not the one using strict.
63
64 A class that inherits from a Moose based class will discover that the
65 Moose class's attributes are disallowed. Given sufficient Moose meta
66 knowledge it might be possible to work around this. I'd appreciate
67 pull requests and or an outline of a solution.
68
69 Subverting strictness
70 MooseX::StrictConstructor documents a trick for subverting strictness
71 using BUILD. This does not work here because strictness is enforced in
72 the early stage of object construction but the BUILD subs are run after
73 the objects has been built.
74
75 Interactions with namespace::clean
76 MooX::StrictConstructor creates a "new" method that namespace::clean
77 will over-zealously clean. Workarounds include using
78 MooX::StrictConstructor after namespace::autoclean or telling
79 namespace::clean to ignore "new" with something like:
80
81 use namespace::clean -except => ['new','meta'];
82
84 · MooX::InsideOut
85
86 · MooseX::StrictConstructor
87
89 George Hartzell <hartzell@cpan.org>
90
92 This software is copyright (c) 2018 by George Hartzell.
93
94 This is free software; you can redistribute it and/or modify it under
95 the same terms as the Perl 5 programming language system itself.
96
97
98
99perl v5.28.1 2018-03-25 MooX::StrictConstructor(3)