1MooX::StrictConstructorU(s3e)r Contributed Perl DocumentaMtoiooXn::StrictConstructor(3)
2
3
4

NAME

6       MooX::StrictConstructor - Make your Moo-based object constructors blow
7       up on unknown attributes.
8

VERSION

10       version 0.011
11

SYNOPSIS

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

DESCRIPTION

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       Your application can use Carp::Always to generate stack traces on
33       "die".  Previously all exceptions contained traces, but this could
34       potentially leak sensitive information, e.g.
35
36           My::Sensitive::Class->new( password => $sensitive, extra_value => 'foo' );
37
38   STANDING ON THE SHOULDERS OF ...
39       Most of this package was lifted from MooX::InsideOut and most of the
40       Role that implements the strictness was lifted from
41       MooseX::StrictConstructor.
42
43   SUBVERTING STRICTNESS
44       MooseX::StrictConstructor documents two tricks for subverting
45       strictness and avoid having problematic arguments cause an exception:
46       handling them in BUILD or handle them in BUILDARGS.
47
48       In MooX::StrictConstructor you can use a BUILDARGS function to handle
49       them, e.g. this will allow you to pass in a parameter called "spy"
50       without raising an exception.  Useful?  Only you can tell.
51
52          sub BUILDARGS {
53              my ($self, %params) = @_;
54              my $spy delete $params{spy};
55              # do something useful with the spy param
56              return \%params;
57          }
58
59       Because "BUILD" methods are run after an object has been constructed
60       and this code runs before the object is constructed the "BUILD" trick
61       will not work.
62

BUGS/ODDITIES

64   Inheritance
65       A class that uses MooX::StrictConstructor but extends another class
66       that does not will not be handled properly.  This code hooks into the
67       constructor as it is being strung up (literally) and that happens in
68       the parent class, not the one using strict.
69
70       A class that inherits from a Moose based class will discover that the
71       Moose class's attributes are disallowed.  Given sufficient Moose meta
72       knowledge it might be possible to work around this.  I'd appreciate
73       pull requests and or an outline of a solution.
74
75   Subverting strictness
76       MooseX::StrictConstructor documents a trick for subverting strictness
77       using BUILD.  This does not work here because strictness is enforced in
78       the early stage of object construction but the BUILD subs are run after
79       the objects has been built.
80
81   Interactions with namespace::clean
82       MooX::StrictConstructor creates a "new" method that namespace::clean
83       will over-zealously clean.  Workarounds include using
84       MooX::StrictConstructor after namespace::autoclean or telling
85       namespace::clean to ignore "new" with something like:
86
87         use namespace::clean -except => ['new','meta'];
88

SEE ALSO

90       ·   MooX::InsideOut
91
92       ·   MooseX::StrictConstructor
93

AUTHOR

95       George Hartzell <hartzell@cpan.org>
96
98       This software is copyright (c) 2020 by George Hartzell.
99
100       This is free software; you can redistribute it and/or modify it under
101       the same terms as the Perl 5 programming language system itself.
102
103
104
105perl v5.30.1                      2020-03-11        MooX::StrictConstructor(3)
Impressum