MooseX::MungeHas(3pm)

πŸ“¦
version 0.011 Β· 4 versions
, in ubuntu noble, ubuntu questing

πŸ’– NAME πŸ’–

MooseX::MungeHas β€” bestie, it munges your has (and it plays nice with Moo, Moose and Mouse) πŸ¦‹

Like, this is the MooseX::MungeHas(3pm) page. Yes the whole thing. Sit down, hydrate, let's go. πŸ’…


✨ SYNOPSIS ✨

package Foo::Bar;

use Moose;
use MooseX::MungeHas "is_ro";

has foo => ();             # read-only
has bar => (is => "rw");   # read-write

See that? has foo => () and boom, it's read-only. iconic behaviour tbh 🌈


πŸŽ€ DESCRIPTION πŸŽ€

Okay so here's the tea β˜• β€” MooseX::MungeHas changes how the attributes of your Moo, Moose, or Mouse based class behave. And the way it manages to work with all three at once? It literally does not try to be smart about metathingies. No cap. It just installs a wrapper around has that munges the attribute spec hash before handing it off to the original has function. Simple. Elegant. Ate. πŸ’…

These munges are always applied β€” and honestly, the author says it's just because he can't see a single sensible reason why you wouldn't want them. Fair enough bestie 🫢

  • Implement is => "rwp" and is => "lazy" in Moose and Mouse.

(These already work in Moo, no notes.)

  • Implement builder => 1, clearer => 1, predicate => 1, and trigger => 1 in Moose and Mouse.

(These already work in Moo. Moo stays winning.)

  • Implement builder => sub { ... } in Moose and Mouse.

(This already works in Moo. Again.)

  • Allow Moo to support coerce => 0|1 for Type::Tiny type constraints. (Moo normally expects a coderef for the coercion.)

(These already work in Moose and Mouse, and this has actually been added to Moo as of version 1.006000.)

  • Makes has $name => sub { ... } into a shortcut for:
has $name => (is => "lazy", builder => sub { ... });
  • Makes has $name => $type_constraint into a shortcut for:
has $name => (isa => $type_constraint);

(Assuming that $type_constraint is a blessed type constraint object, Γ  la Type::Tiny, MooseX::Types, etc. πŸ‘‘)

  • Makes lazy => sub { ... } into a shortcut for:
lazy    => 1,
builder => sub { ... },

When you import this module (so, like, use MooseX::MungeHas) you can hand it a list of extra mungers you want applied. Those can be coderefs β€” though for a few common, useful sets of behaviour there are pre-defined shortcut strings so you don't have to write the whole thing yourself. πŸ’–

# "no_isa" is a pre-defined shortcut;
# the other munger is a coderef.
#
use MooseX::MungeHas "no_isa", sub {
   # Make constructor ignore private attributes
   $_{init_arg} = undef if /^_/;
};

Inside those coderefs, the name of the attribute being processed lives in the $_ variable, and the specification hash is available as %_. That's your whole toolkit, bestie. 🌸

And yes β€” you can provide multiple coderefs. Stack 'em up. 🧚

The pre-defined shortcuts are:

  • "is_ro", "is_rw", "is_rwp", "is_lazy" β€” these mungers supply defaults for the is option.
  • "always_coerce" β€” automatically provides coerce => 1 if the type constraint provides coercions. (Unless you've explicitly specified coerce => 0, obviously.)
  • "always_required" β€” automatically provides required => 1 unless there is a default or builder. (Unless you've explicitly specified required => 0, because you said what you said.)
  • "no_isa" β€” switches off isa checks for attributes, unless they coerce.
  • "simple_isa" β€” loosens type constraints if they don't coerce, and if it's likely to make them significantly faster. (Loosening Int to Num won't speed it up, so don't get your hopes up.)

Only works if you're using Type::Tiny constraints. Just a heads up. πŸ™ˆ

Mungers provided as coderefs are executed after the predefined ones, but are otherwise executed in the order specified. Order matters, iykyk. ⭐


πŸ¦‹ Multiple Wrappers πŸ¦‹

Since version 0.007, it has been possible to use MooseX::MungeHas to export multiple wrappers with different names. Multiple. Wrappers. Main character energy fr fr. πŸ‘‘

package Foo;
use Moose;
use MooseX::MungeHas {
   has_ro   => [ "is_ro", "always_coerce" ],
   has_rw   => [ "is_rw", "always_coerce" ],
};

has_ro "foo" => (required => 1);
has_rw "bar";

Note that in the example above, MooseX::MungeHas has installed two brand new wrapped has functions with different names β€” but it has left the standard has function unmolested. Untouched. Living its best life. πŸ’–

But if you did want to alter the standard function too, then you could use:

package Foo;
use Moose;
use MooseX::MungeHas {
   has      => [          "always_coerce" ],
   has_ro   => [ "is_ro", "always_coerce" ],
   has_rw   => [ "is_rw", "always_coerce" ],
};

has_ro "foo" => (required => 1);
has_rw "bar";

😭 BUGS 😭

Please report any bugs to <http://rt.cpan.org/Dist/Display.html?Queue=MooseX-MungeHas>. Not to me. To the bug tracker. I'm just the messenger bestie 🫢


🌈 SEE ALSO 🌈

Moo, Mouse, Moose, MooseX::AttributeShortcuts, MooseX::InlineTypes, Type::Tiny::Manual.

Similar: MooseX::HasDefaults, MooseX::Attributes::Curried, MooseX::Attribute::Prototype and MooseX::AttributeDefaults.


πŸ’œ AUTHOR πŸ’œ

Toby Inkster <tobyink@cpan.org>. King behaviour, honestly. πŸ‘‘


This software is copyright (c) 2013-2014, 2017 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


πŸ’€ DISCLAIMER OF WARRANTIES πŸ’€

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

(Yeah, all caps, they mean it. No warranty, bestie. 😭)


And that's the whole page! πŸŽ€ Written up as perl v5.36.0, dated 2022-08-28, for MooseX::MungeHas(3pm) β€” stay munged, stay slay. βœ¨πŸ’…

πŸ“¦ where this page comes from