MooX::R...eterized(3)

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

πŸ’– MooX::Role::Parameterized πŸ’–

okay bestie, welcome to MooX::Role::Parameterized(3) β€” a page from the User Contributed Perl Documentation collection, and trust, it's a whole vibe. Roles. With parameters. Let's get into it ✨


✨ NAME ✨

MooX::Role::Parameterized β€” roles with composition parameters. that's it, that's the name, and it's lowkey iconic πŸŽ€


🌸 SYNOPSIS 🌸

here's the whole thing in action, no cap:

package Counter;
use Moo::Role;
use MooX::Role::Parameterized;
use Types::Standard qw( Str );

parameter name => (
    is       => 'ro',  # this is mandatory on Moo
    isa      => Str,   # optional type
    required => 1,     # mark the parameter "name" as "required"
);

role {
    my ( $p, $mop ) = @_;

    my $name = $p->name; # $p->{name} will also work

    $mop->has($name => (
        is      => 'rw',
        default => sub { 0 },
    ));

    $mop->method("increment_$name" => sub {
        my $self = shift;
        $self->$name($self->$name + 1);
    });

    $mop->method("reset_$name" => sub {
        my $self = shift;
        $self->$name(0);
    });
};

package MyGame::Weapon;
use Moo;
use MooX::Role::Parameterized::With;

with Counter => {          # injects 'enchantment' attribute and
    name => 'enchantment', # methods increment_enchantment ( +1 )
};                         # reset_enchantment (set to zero)

package MyGame::Wand;
use Moo;
use MooX::Role::Parameterized::With;

with Counter => {         # injects 'zapped' attribute and
    name => 'zapped',     # methods increment_zapped ( +1 )
};                        # reset_zapped (set to zero)

like… one role, two classes, two whole different attribute names. that's the ✨magic✨ πŸ¦‹


πŸ’… DESCRIPTION πŸ’…

It is an experimental port of MooseX::Role::Parameterized to Moo. experimental, bestie. she's still figuring herself out, but she's doing great 🫢


πŸ‘‘ FUNCTIONS πŸ‘‘

This package exports the following subroutines: parameter, role, apply_roles_to_target and apply. four of them. all yours. periodt πŸ’œ

πŸ“ parameter

This function receive the same parameter as Moo::has. If present, the parameter hash reference will be blessed as a Moo class. This is useful to add default values or set some parameters as required. so like, you can make your params real little objects with defaults and required and everything. slay πŸ’–

🧚 role

This function accepts just one code block. Will execute this code then we apply the Role in the target classand will receive the parameter hash reference + one mop object.

The params reference will be blessed if there is some parameter defined on this role.

The mop object is a proxy to the target class.

It offer a better way to call has, after, before, around, with and requires without side effects. no side effects!! we love a clean application, fr fr ✨

Use method to inject a new method and meta to access TARGET_PACKAGE->meta

Please use:

my ($params, $mop) = @_;
...
$mop->has($params->{attribute} =>(...));

$mop->method(name => sub { ... });

$mop->meta->make_immutable;

that's the whole recipe. $params is your stuff, $mop is your tool. go off πŸ’…

🌈 apply

Alias to apply_roles_to_target. two names, one function, main character energy πŸ‘‘

⭐ apply_roles_to_target

When called, will apply the /role on the current package. The behavior depends of the parameter list.

This will install the role in the target package. Does not need call with.

Important, if you want to apply the role multiple times, like to create multiple attributes, please pass an arrayref.

so listen. one call, many roles, arrayref. got it? here's the receipts:

package My::Role;

use Moo::Role;
use MooX::Role::Parameterized;

role {
    my ($params, $mop) = @_;

    $mop->has( $params->{attr} => ( is => 'rw' ));

    $mop->method($params->{method} => sub {
        1024;
    });
};

package My::Class;

use Moo;
use My::Role;

My::Role->apply_roles_to_target([{ # original way of add this role
    attr   => 'baz',               # add attribute read-write called 'baz'
    method => 'run'                # add method called 'run' and return 1024
}
 ,                                 # and if the apply receives one arrayref
{   attr   => 'bam',               # will call the role block multiple times.
    method => 'jump'               # PLEASE CALL apply once
}]);
PLEASE CALL apply once β€” the original is yelling it in all caps and honestly? she's right. don't do it twice. don't. πŸ’€

πŸŽ€ STATIC METHOS πŸŽ€

(yes the heading says "METHOS". we're keeping it. it's canon now πŸ™ˆ)

πŸ¦‹ is_role

Returns true if the package is a MooX::Role::Parameterized.

MooX::Role::Parameterized->is_role("My::Role");

one line, one answer, true or false. vibe check complete βœ…


πŸ’€ DEPRECATED FUNCTIONS πŸ’€

these are gone. deceased. not coming back. no cap.

hasp

Removed.

method

Removed.

like… don't even try, bestie. they're not there 😭


🌸 VARIABLES 🌸

πŸ’œ MooX::Role::Parameterized::VERBOSE

By setting $MooX::Role::Parameterized::VERBOSE with some true value we will carp on certain conditions (method override, unable to load package, etc).

Default is false.

so if you want her to talk about what's going wrong, flip that to true and she'll carp. default is quiet. default is false. periodt ✨


🧚 MooX::Role::Parameterized::With 🧚

See MooX::Role::Parameterized::With package to easily load and apply roles. that's the one that makes with just… work. obsessed 🫢


πŸ”₯ SEE ALSO πŸ”₯

  • MooseX::Role::Parameterized β€” Moose version. the original. the blueprint. the mother πŸ‘‘

🌈 THANKS 🌈

big love to these absolute legends, fr fr:

  • FGA <fabrice.gabolde@gmail.com>
  • PERLANCAR <perlancar@gmail.com>
  • CHOROBA <choroba@cpan.org>
  • Ed J <mohawk2@users.noreply.github.com>

thank you for your service πŸ«‘πŸ’–


πŸ“œ LICENSE πŸ“œ

The MIT License

The MIT License

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated
 documentation files (the "Software"), to deal in the Software
 without restriction, including without limitation the rights to
 use, copy, modify, merge, publish, distribute, sublicense,
 and/or sell copies of the Software, and to permit persons to
 whom the Software is furnished to do so, subject to the
 following conditions:

  The above copyright notice and this permission notice shall
  be included in all copies or substantial portions of the
  Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT
   WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
   INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   MERCHANTABILITY, FITNESS FOR A PARTICULAR
   PURPOSE AND NONINFRINGEMENT. IN NO EVENT
   SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
   LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR
   OTHER DEALINGS IN THE SOFTWARE.

✨ AUTHOR ✨

Tiago Peczenyj <tiago (dot) peczenyj (at) gmail (dot) com>


πŸ› BUGS πŸ›

Please report any bugs or feature requests on the bugtracker website. found a bug? tell the bugtracker, not the group chat bestie πŸ’Œ


and that's the whole page!! this was documented with perl v5.42.0, dated 2025-07-25, and it's the MooX::R...eterized(3) manpage, aka MooX::Role::Parameterized from the User Contributed Perl Documentation. love that for us. mwah πŸ’…πŸ’–βœ¨

πŸ“¦ where this page comes from