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>

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