MooseX...olerant(3pm)

πŸ“¦
version 0.21 Β· 3 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu questing

πŸ’– 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 passing undef and everything just... being fine? no explosion, no drama, has_name says 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>

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