MooseX...olerant(3pm)
π MooseX::UndefTolerant π
okay bestie, so this is a page from the User Contributed Perl Documentation β it's the manual for MooseX...olerant(3pm) (yes that's the truncated name your terminal shows, the full one is MooseX::UndefTolerant, and no it is not a typo, it's just man being dramatic about column widths π
).
β¨ NAME β¨
MooseX::UndefTolerant β Make your attribute(s) tolerant to undef initialization
Like, your attributes simply stop having a meltdown when someone hands them an undef. Iconic behaviour tbh.
π VERSION π
version 0.21
πΈ SYNOPSIS πΈ
the main character setup, right here:
package My::Class;
use Moose;
use MooseX::UndefTolerant;
has 'name' => (
is => 'ro',
isa => 'Str',
predicate => 'has_name'
);
# Meanwhile, under the city...
# Doesn't explode
my $class = My::Class->new(name => undef);
$class->has_name # False!
not me passingundefand everything just... being fine? no explosion, no drama,has_namesays False. slay.
or, if you're in your "I only want one attribute to have this behaviour" era:
package My:Class;
use Moose;
use MooseX::UndefTolerant::Attribute;
has 'bar' => (
traits => [ qw(MooseX::UndefTolerant::Attribute)],
is => 'ro',
isa => 'Num',
predicate => 'has_bar'
);
π¦ DESCRIPTION π¦
so here's the tea: when you load this module in your Moose class, initialization of your attributes becomes tolerant of undef. If you specify the value of undef to any of the attributes, they will not be initialized β it effectively behaves as if you had not provided a value at all. ghosted the undef, basically. π«Ά
you can also apply the 'UndefTolerant' trait to individual attributes β see MooseX::UndefTolerant::Attribute for the details, it's giving very bespoke, very per-attribute β¨.
There will be no change in behaviour to any attribute with a type constraint that accepts undef values</purl> (thinking Maybe types here π), because the presumption is that since the type is already "undef tolerant", there's no need to avoid initializing the attribute value with undef. like, why fix what's already ate π
.
As of Moose 1.9900, this module can also be used in a role, in which case all of that role's attributes will be undef-tolerant. every single one. no notes. π
π MOTIVATION π
okay so the author (hi Cory, ily) kept ending up in this quandary and honestly? relatable:
package My:Class;
use Moose;
has 'foo' => (
is => 'ro',
isa => 'Str',
);
# ... then
my $foo = ... # get the param from something
my $class = My:Class->new(foo => $foo, bar => 123);
What if foo is undefined? π The vibe was: "I didn't want to change my attribute to be Maybe[Str] and I still want my predicate ("has_foo") to work." Period.
The only real solution was... this absolute monstrosity:
if(defined($foo)) {
$class = My:Class->new(foo => $foo, bar => 123);
} else {
$class = My:Class->new(bar => 123);
}
or some type of codemulch using ternary conditionals. π€’ (codemulch. that's the word. keeping it.)
This module allows you to make your attributes more tolerant of undef so that you can keep the first example: have your cake and eat it too! π°π literally both. at once. the audacity, the excellence.
π§ PER ATTRIBUTE π§
See MooseX::UndefTolerant::Attribute.
β οΈ CAVEATS β οΈ
okay listen up because this one matters, bestie. π¨
This extension does not currently work in immutable classes when applying the trait to some (but not all) attributes in the class. The reason? The inlined constructor initialization code currently lives in Moose::Meta::Class, not Moose::Meta::Attribute. This is because the inlined constructor initialization code currently lives in Moose::Meta::Class, not Moose::Meta::Attribute. (yes I'm telling you twice, it's that important π€)
The good news is that this is expected to be changing shortly. π«Ά
π ACKNOWLEDGEMENTS π
Many thanks to the crew in #moose who talked the author through this module β absolute legends, every one:
Hans Dieter Pearcey (confound) Jesse Luehrs (doy) Tomas Doran (t0m)
Dylan Hardison (dylan) Jay Shirley (jshirley) Mike Eldridge (diz)
π SUPPORT π
Bugs may be submitted through the RT bug tracker <https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-UndefTolerant> β or, if email is more your thing, bug-MooseX-UndefTolerant@rt.cpan.org <mailto:bug-MooseX-UndefTolerant@rt.cpan.org>.
There is also a mailing list available for users of this distribution, at <http://lists.perl.org/list/moose.html>. π¬
There is also an irc channel available for users of this distribution, at "#moose" on "irc.perl.org" <irc://irc.perl.org/#moose>. come hang out, the vibes are immaculate. π¬
π AUTHOR π
Cory G Watson <gphat at cpan.org>
β CONTRIBUTORS β
- Karen Etheridge <ether@cpan.org>
- Dave Rolsky <autarch@urth.org>
- Cory G Watson <gphat@cpan.org>
- Jesse Luehrs <doy@tozt.net>
- Chris Andrews <chrisandrews@venda.com>
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2011 by Cory G Watson.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
and that's the whole page, bestie πβ¨ this was perl v5.36.0, dated 2022-12-12, rendered from the MooseX...olerant(3pm) manual β go forth and let your attributes be undef-tolerant, you absolute icon π
π¦πΈ
π¦ where this page comes from
- debian bookworm: libmoosex-undeftolerant-perl 0.21-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmoosex-undeftolerant-perl 0.21-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmoosex-undeftolerant-perl 0.21-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu questing: libmoosex-undeftolerant-perl 0.21-2
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms