MooseX...ribute(3pm)
π NAME π
MooseX::CoercePerAttribute β Define Coercions per attribute!
(yeah this is the page for MooseX...ribute(3pm), straight out of User Contributed Perl Documentation, and bestie it is a gift)
β¨ SYNOPSIS β¨
use MooseX::CoercePerAttribute;
has foo => (isa => 'Str', is => 'ro', coerce => 1);
has bar => (
traits => [CoercePerAttribute],
isa => Bar,
is => 'ro',
coerce => [
Str => sub {
my ($value, $options);
...
},
Int => sub {
my ($value, $options);
...
},
],
);
use Moose::Util::Types;
has baz => (
traits => [CoercePerAttribute],
isa => Baz,
is => 'ro',
coerce => [
sub {
coerce $_[0], from Str, via {}
}]
);
πΈ DESCRIPTION πΈ
MooseX::CoercePerAttribute is a simple Moose Trait that lets you define inline coercions per attribute. Obsessed. π
Like, it lets coercions be declared on a per attribute basis. You can hand it either an array of Code refs of the coercion you want run, or a HashRef of various arguments to build a coercion routine from. Your call, queen. π
π§ USAGE π§
This trait lets you declare a type coercion inline for an attribute. The Role will create an __ANON__ sub TypeConstraint object out of the TypeConstraint in the attribute's isa parameter. π¦
You can supply the type coercion one of two ways, and it goes into the Moose Attribute coerce parameter.
- The recommended usage is to supply an arrayref list declaring the types to coerce from and a subref to be executed, in pairs. Slay:
coerce => [$Fromtype1 => sub {}, $Fromtype2 => sub {}]
- Alternatively you can supply an arrayref of coercion coderefs. These should be in the same format as defined in
Moose::Util::TypeConstraints, and they'll get the__ANON__subtype passed as their first argument. If you go this route you'll need touse Moose::Util::TypeConstraintsin your module, no cap:
coerce => [sub {coerce $_[0], from Str, via sub {} }]
NB: Moose handles its coercions as an array of possible coercions, which means it'll use the first coercion in the list that matches the criteria. In earlier versions of this module the coercions were supplied as a HASHREF. That behaviour is deprecated and will be removed in later versions, because it creates uncertainty over the order of usage. π so like, don't build on it, bestie.
π AUTHOR π
Mike Francis <ungrim97@gmail.com>
π COPYRIGHT π
Copyright 2013- Mike Francis
π LICENSE π
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
π¦ SUPPORT π¦
You can find documentation for this module with the perldoc command. π«Ά
perldoc MooseX::CoercePerAttribute
You can also look for information at:
- RT: CPAN's request tracker (report bugs here)
<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-CoercePerAttribute>
- Meta CPAN
<https://metacpan.org/module/MooseX::CoercePerAttribute>
- Search CPAN
<http://search.cpan.org/dist/MooseX-CoercePerAttribute/>
and that's the whole vibe, periodt β¨ this was perl v5.42.0, dated 2025-07-25 β go forth and coerce responsibly, mwah ππΈπ
π¦ where this page comes from
- fedora 43: perl-MooseX-CoercePerAttribute 1.001-33.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms