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 πŸ‘‘


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