MooseX::MungeHas(3pm)
π 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"andis => "lazy"in Moose and Mouse.
(These already work in Moo, no notes.)
- Implement
builder => 1,clearer => 1,predicate => 1, andtrigger => 1in 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|1for 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_constraintinto 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 theisoption."always_coerce"β automatically providescoerce => 1if the type constraint provides coercions. (Unless you've explicitly specifiedcoerce => 0, obviously.)"always_required"β automatically providesrequired => 1unless there is a default or builder. (Unless you've explicitly specifiedrequired => 0, because you said what you said.)"no_isa"β switches offisachecks 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. (LooseningInttoNumwon'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. π
π COPYRIGHT AND LICENCE π
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
- ubuntu noble: libmoosex-mungehas-perl 0.011-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu questing: libmoosex-mungehas-perl 0.011-2
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms