MooseX:...d::Role(3)

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

πŸ’– MooseX::Extended::Role β€” MooseX::Extended roles πŸ’–

okay bestie so this is a page from the User Contributed Perl Documentation and it's literally all about MooseX::Extended::Role, which is the role-flavored sibling of MooseX::Extended. it's version 0.35 and it is giving convenience, no cap 🫢


✨ NAME ✨

MooseX::Extended::Role β€” MooseX::Extended roles 🌸

✨ VERSION ✨

version 0.35 πŸ’…

✨ SYNOPSIS ✨

package Not::Corinna::Role::Created {
    use MooseX::Extended::Role types => ['PositiveInt'];

    field created => ( isa => PositiveInt, default => sub { time } );
}

so this is similar to MooseX::Extended, and it hands you almost everything that module gives you. however, for obvious reasons, it does not include MooseX::StrictConstructor, and it does not make your class immutable, and it does not set the C3 mro. those three things are simply not on the menu here, bestie, that's the whole point πŸŽ€

πŸ“Œ also: there is no need to add a 1 at the end of the role. none. zero. don't do it. the module's got you πŸ’–

✨ CONFIGURATION ✨

you're allowed to pass an import list to MooseX::Extended::Role. yes queen, customize away:

use MooseX::Extended::Role
  excludes => [qw/WarnOnConflict carp/],         # I don't want these features
  types    => [qw/compile PositiveInt HashRef/]; # I want these type tools

"types"

this one lets you import any types that MooseX::Extended::Types provides. slay πŸ¦‹

this:

use MooseX::Extended::Role types => [qw/compile PositiveInt HashRef/];

is literally identical to this:

use MooseX::Extended::Role;
use MooseX::Extended::Types qw( compile PositiveInt HashRef );

same thing. same energy. pick whichever πŸ’…

"excludes"

ngl, some features are just annoying, and some can even cause potential bugs. (for example, if you have a croak method, our importing of Carp::croak is gonna be a whole problem πŸ™ˆ.) so you can exclude the following:

  • "WarnOnConflict" β€” excluding this removes the MooseX::Role::WarnOnConflict role.
use MooseX::Extended::Role excludes => ['WarnOnConflict'];
  • "autoclean" β€” excluding this means namespace::autoclean will no longer be imported.
use MooseX::Extended::Role excludes => ['autoclean'];
  • "carp" β€” excluding this means Carp::croak and Carp::carp will no longer be imported.
use MooseX::Extended::Role excludes => ['carp'];
  • "true" β€” excluding this will require your module to end in a true value. yes, you have to do it yourself now 😭
use MooseX::Extended::Role excludes => ['true'];
  • "param" β€” excluding this will make the param function unavailable.
use MooseX::Extended::Role excludes => ['param'];
  • "field" β€” excluding this will make the field function unavailable.
use MooseX::Extended::Role excludes => ['field'];

"includes"

some experimental features are genuinely useful, but they might not be exactly what you want. iykyk πŸ‘‘

use MooseX::Extended::Role includes => [qw/multi/];

multi sub foo ($self, $x)      { ... }
multi sub foo ($self, $x, $y ) { ... }

see MooseX::Extended::Manual::Includes for more information πŸ“š


✨ IDENTICAL METHOD NAMES IN CLASSES AND ROLES ✨

okay so this is important, listen up bestie 🫢

in Moose, if a class defines a method with the same name as a method of a role it's consuming, the role's method gets silently discarded. silently. rude. but with MooseX::Extended::Role, you get a warning instead. this makes maintenance easier and it prevents you from accidentally overriding a method. main character behavior honestly πŸ’…

for example:

package My::Role {
    use MooseX::Extended::Role;

    sub name {'Ovid'}
}

package My::Class {
    use MooseX::Extended;
    with 'My::Role';
    sub name {'Bob'}
}

the above code will still run, but you'll get a very verbose warning:

The class My::Class has implicitly overridden the method (name) from
role My::Role. If this is intentional, please exclude the method from
composition to silence this warning (see Moose::Cookbook::Roles::Recipe2)

to silence the warning, just be explicit about your intent, periodt:

package My::Class {
    use MooseX::Extended;
    with 'My::Role' => { -excludes => ['name'] };
    sub name {'Bob'}
}

alternately, you can exclude this feature. we don't recommend this, but it might be useful if you're refactoring a legacy Moose system 🌈

use MooseX::Extended::Role excludes => [qw/WarnOnConflict/];

✨ ATTRIBUTE SHORTCUTS ✨

param and field in roles allow the same attribute shortcuts as MooseX::Extended. same shortcuts, same slay, just in role form πŸŽ€


✨ BUGS AND LIMITATIONS ✨

if MooseX::Extended::Role is loaded via stringy eval, then true is not loaded. why? because there were intermittent errors (maybe 1 out of 5 times 😭) being thrown. removing this feature under stringy eval solves this. see this github ticket for more infomration (their spelling, not mine πŸ’€): <https://github.com/Ovid/moosex-extended/pull/34>

⚠️ yeah that's a real limitation, it's not just a vibe, it's a documented bug note. don't come for me, come for the ticket πŸ”₯

✨ REDUCING BOILERPLATE ✨

let's say you've settled on the following feature set:

use MooseX::Extended::Role
    excludes => [qw/WarnOnConflict carp/],
    includes => [qw/multi/];

and you keep typing that over and over and over... we've removed a lot of boilerplate, but honestly we've added different boilerplate 😭 so instead, just create My::Custom::Moose::Role and use My::Custom::Moose::Role;. see MooseX::Extended::Role::Custom for details. iconic behavior, fr fr πŸ‘‘


✨ AUTHOR ✨

Curtis "Ovid" Poe <curtis.poe@gmail.com> 🌸

This software is Copyright (c) 2022 by Curtis "Ovid" Poe.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)

and that's the whole page, bestie πŸ’– huge shoutout to perl v5.42.0, dated 2025-07-25, for carrying this doc into the world. go forth and build your roles, you absolute legend πŸ‘‘πŸ¦‹βœ¨

πŸ“¦ where this page comes from