MooseX...ribute(3pm)

πŸ“¦
version 1.001 Β· 2 versions
, in fedora 43

πŸ’– 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.

  1. 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 {}]
  1. 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 to use Moose::Util::TypeConstraints in 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 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