MooX::...tructor(3pm)
π MooX::StrictConstructor π
okay bestie, welcome to MooX::...tructor(3pm) β yes that's the man name, it's a whole vibe, don't worry about it β¨ this is the page for MooX::StrictConstructor, aka the module that makes your Moo-based object constructors blow up on unknown attributes. no cap, it's giving bodyguard energy π«Ά
πΈ NAME πΈ
MooX::StrictConstructor β Make your Moo-based object constructors blow up on unknown attributes π₯
like. it really said "unexpected args? not on my watch."
β¨ SYNOPSIS β¨
package My::Class;
use Moo;
use MooX::StrictConstructor;
has 'size' => ( is => 'rw' );
# then somewhere else, when constructing a new instance
# of My::Class ...
# this blows up because color is not a known attribute
My::Class->new( size => 5, color => 'blue' );
see how that last line is a whole crime scene? color was never declared, so it's not invited to the party π
ββοΈ and the constructor is gonna die about it.
π¦ DESCRIPTION π¦
Simply loading this module makes your constructors "strict". that's it, that's the whole thing. one use and suddenly your objects have boundaries (as they should π
).
so the tea is: if your constructor gets called with an attribute init argument that your class does not declare, then it dies. periodt. and honestly? this is a great way to catch small typos, because bestie nothing is more embarrassing than size vs szie and then you're debugging for three hours when the bug was right there the whole time π
π STANDING ON THE SHOULDERS OF ... π
This module was inspired by MooseX::StrictConstructor, and includes some implementation details taken from it. so like, respect where respect is due π€ iconic lineage fr fr
π SUBVERTING STRICTNESS π
okay so sometimes you're being deliberate about weird args and you need a way out. there are two options for subverting the strictness to handle problematic arguments. They can be handled in BUILDARGS or in BUILD. pick your fighter π₯
You can use a BUILDARGS function to handle them, e.g. this will allow you to pass in a parameter called spy without raising an exception. Useful? Only you can tell. (no fr, only you can tell, we can't gatekeep your architecture π«Ά)
sub BUILDARGS {
my ($self, %params) = @_;
my $spy = delete $params{spy};
# do something useful with the spy param
return \%params;
}
It is also possible to handle extra parameters in BUILD. This requires the strictness check to be performed at the end of object construction rather than at the beginning. so basically you're checking the vibe after the party instead of at the door πͺ
use MooX::StrictConstuctor -late;
sub BUILD {
my ($self, $params) = @_;
if ( my $spy = delete $params->{spy} ) {
# do something useful
}
}
β οΈ heads up bestie: When using this option, the object will be fully constructed before checking the parameters, and a failure will cause the destructor to be run. so like, real consequences, not a drill π
π BUGS/ODDITIES π
𧬠Inheritance π§¬
A class that uses MooX::StrictConstructor but extends a non-Moo class will not be handled properly. This code hooks into the constructor as it is being strung up (literally) and that happens in the parent class, not the one using strict. so like, the strictness is in the wrong room, it's giving "showed up to the wrong address" energy π
A class that inherits from a Moose based class will discover that the Moose class's attributes are disallowed. Given sufficient Moose meta knowledge it might be possible to work around this. I'd appreciate pull requests and or an outline of a solution. (author is literally asking, bestie, if you know, you know β iykyk π§)
π§Ό Interactions with namespace::clean π§Ό
MooX::StrictConstructor creates a "new" method that namespace::clean will over-zealously clean. Workarounds include using namespace::autoclean, using MooX::StrictConstructor after namespace::clean or telling namespace::clean to ignore "new" with something like:
use namespace::clean -except => ['new'];
yeah it's the classic "roommate threw away my stuff while tidying" situation π§Ή not malicious, just too thorough.
π SEE ALSO π
MooseX::StrictConstructor
π BUGS π
Please report any bugs or feature requests on the bugtracker website <https://rt.cpan.org/Public/Dist/Display.html?Name=MooX-StrictConstructor> or by email to bug-MooX-StrictConstructor@rt.cpan.org <mailto:bug-MooX-StrictConstructor@rt.cpan.org>.
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. pls pls pls include the test file, the maintainers are begging, it's the bare minimum and it makes you the main character π
π AUTHOR π
George Hartzell <hartzell@cpan.org>
π CONTRIBUTORS π
- George Hartzell <hartzell@alerce.com>
- Graham Knop <haarg@haarg.org>
- JJ Merelo <jjmerelo@gmail.com>
- jrubinator <jjrs.pam+github@gmail.com>
- mohawk2 <mohawk2@users.noreply.github.com>
- Samuel Kaufman <samuel.c.kaufman@gmail.com>
- Tim Bunce <Tim.Bunce@pobox.com>
squad assembled, icons only π
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2013 by George Hartzell.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. slay, free software fr fr π«Ά
and that's the page bestie!! π this was perl v5.40.1 doing its thing, dated 2025-10-05, and if you ever forget where you read all this, it's the manpage formerly known as MooX::...tructor(3pm) π
β¨ go forth and be strict, your future self debugging at 2am will literally thank you πΈ
π¦ where this page comes from
- ubuntu resolute: libmoox-strictconstructor-perl 0.013-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmoox-strictconstructor-perl 0.013-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms