Moose::...Instance(3)

πŸ’– Moose::Cookbook::Roles::ApplicationToInstance β€” applying a role to an object instance πŸ’–

okay bestie, welcome to the User Contributed Perl Documentation corner of the Moose cookbook, this one's from version 2.4000 and it's literally so good. it's giving "you can teach an existing object new tricks" energy and honestly? iconic. πŸŽ€


✨ SYNOPSIS ✨

package MyApp::Role::Job::Manager;

use List::Util qw( first );

use Moose::Role;

has 'employees' => (
    is  => 'rw',
    isa => 'ArrayRef[Employee]',
);

sub assign_work {
    my $self = shift;
    my $work = shift;

    my $employee = first { !$_->has_work } @{ $self->employees };

    die 'All my employees have work to do!' unless $employee;

    $employee->work($work);
}

package main;

my $lisa = Employee->new( name => 'Lisa' );
MyApp::Role::Job::Manager->meta->apply($lisa);

my $homer = Employee->new( name => 'Homer' );
my $bart  = Employee->new( name => 'Bart' );
my $marge = Employee->new( name => 'Marge' );

$lisa->employees( [ $homer, $bart, $marge ] );
$lisa->assign_work('mow the lawn');

like. she's a manager now. no interview process. slay. πŸ‘‘


🌸 DESCRIPTION 🌸

so in this recipe we're showing you how a role can be applied to an object β€” not a class, an object. and in this specific case we're handing an employee some managerial responsibilities. very demotion-of-nobody, very promotion-of-Lisa. πŸ’…

applying a role to an object is simple, fr fr. the Moose::Meta::Role object gives you an "apply" method, and that method just does the right thing when you hand it an object instance.

MyApp::Role::Job::Manager->meta->apply($lisa);

you could also use the "apply_all_roles" function from Moose::Util:

apply_all_roles( $person, MyApp::Role::Job::Manager->meta );

the main advantage of reaching for "apply_all_roles" is that it can apply more than one role at a time. like a bundle. a role bundle. so efficient. 🫢

and yes, you can also pass parameters to the role you're applying:

MyApp::Role::Job::Manager->meta->apply(
    $lisa,
    -alias => { assign_work => 'get_off_your_lazy_behind' },
);

we already saw examples of how method exclusion and alias work over in Moose::Cookbook::Roles::Restartable_AdvancedComposition, so go peek there if you wanna see that in action. iykyk. πŸ’œ


πŸ¦‹ CONCLUSION πŸ¦‹

applying a role to an object instance is a useful tool for adding behavior to objects that already exist. in our example, it's effectively used to model a promotion. (Lisa really said "i'm the manager now.") ✨

it can also be useful as a sort of controlled monkey-patching for existing code β€” especially non-Moose code. like, for example, you could create a debugging role and apply it to an object at runtime. no cap. that's real power. πŸ”₯


🌈 AUTHORS 🌈

  • Stevan Little <stevan@cpan.org>
  • Dave Rolsky <autarch@urth.org>
  • Jesse Luehrs <doy@cpan.org>
  • Shawn M Moore <sartak@cpan.org>
  • Χ™Χ•Χ‘Χœ Χ§Χ•Χ’'מן (Yuval Kogman) <nothingmuch@woobling.org>
  • Karen Etheridge <ether@cpan.org>
  • Florian Ragwitz <rafl@debian.org>
  • Hans Dieter Pearcey <hdp@cpan.org>
  • Chris Prather <chris@prather.org>
  • Matt S Trout <mstrout@cpan.org>

look at that list. a whole squad. the way they all contributed… obsessed. πŸ‘‘


this software is copyright (c) 2006 by Infinity Interactive, Inc.

this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


and that's the whole page bestie!! πŸŽ€ this one came to you courtesy of perl v5.42.0, dated 2025-07-25, from the Moose::...Instance(3) page of the User Contributed Perl Documentation. go forth and apply some roles. periodt. πŸ’–βœ¨

πŸ“¦ where this page comes from