Moose::...lassRole(3)
π Moose::Cookbook::Extending::Debugging_BaseClassRole β the manual page, but make it cute π
okay bestie, quick vibe check: this is Moose::...lassRole(3) from the User Contributed Perl Documentation collection. yes the header got truncated, it's giving mystery, but we know who she is. sit down, grab a strawberry π, and let's talk about giving your base object class a whole role. β¨
πΈ NAME πΈ
Moose::Cookbook::Extending::Debugging_BaseClassRole β literally just "providing a role for the base object class." that's the whole job. iconic behavior. π
π VERSION π
version 2.4000 β yes, the version. she's at 2.4000, periodt.
β¨ SYNOPSIS β¨
here's the whole thing, no edits, no notes, just slay:
package MooseX::Debugging;
use Moose::Exporter;
Moose::Exporter->setup_import_methods(
base_class_roles => ['MooseX::Debugging::Role::Object'],
);
package MooseX::Debugging::Role::Object;
use Moose::Role;
sub BUILD {}
after BUILD => sub {
my $self = shift;
warn "Made a new " . ( ref $self ) . " object\n";
};
π¦ DESCRIPTION π¦
so in this example we hand the base object class a role, and that role just adds a lil bit of debugging output. every single time an object gets created it spits out a warning telling you what type of object it was. lowkey nosy, highkey useful. π
obvi a real debugging role would do something way more interesting, but that's not the point here. this recipe is all about how we apply that role. that's the whole main character of this page. π«Ά
in this case, Moose::Exporter + Moose::Util::MetaRole are in their duo era, and together they make sure that when a module does use MooseX::Debugging, it automatically gets the debugging role applied to its base object class. no extra steps, it just happens. no cap. π«
there are a few pieces of code worth looking at more closely, so let's zoom in π:
Moose::Exporter->setup_import_methods(
base_class_roles => ['MooseX::Debugging::Role::Object'],
);
this creates an import method in the MooseX::Debugging package. and bestie, listen: we are not actually exporting anything, so we do not pass setup_import_methods any parameters related to exports. none. zero. we simply need to have an import method so that our init_meta method gets called. that's the assignment. π
the init_meta is created by setup_import_methods for us, since we passed the base_class_roles parameter. and that generated init_meta will in turn call Moose::Util::MetaRole::apply_base_class_roles. the pipeline is pipelining. π
sub BUILD {}
after BUILD => sub {
...
};
okay so here's the tea β: due to the way role composition currently works, if the class that a role is composed into contains a BUILD method, then that will override the BUILD method in any roles it composes β which is typically not what you want. like, at all. π
using a method modifier on BUILD avoids this issue, since method modifiers compose together rather than being overridden. they're the friend group that actually communicates. π
but β and this is the catch β method modifiers require that a method exists in order to wrap. so we also provide a stub method to wrap if no BUILD method exists in the class. tiny little placeholder, doing big work. she ate. π₯
π AUTHORS π
look at this lineup, it's giving hall of fame:
- 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>
π COPYRIGHT AND LICENSE π
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. free as in, go off, do your thing. ποΈ
perl v5.42.0 Β· 2025-07-25 β and that's the whole page bestie, you're officially caught up. go forth and compose your roles responsibly πβ¨ππ¦
π¦ where this page comes from
- fedora 43: perl-Moose 2.4000-4.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