Moose::...lacement(3)
π Moose::Cookbook::Legacy::Debugging_BaseClassReplacement π
okay so bestie, welcome to the User Contributed Perl Documentation (section 3, iconic), where we're reading up on Moose::Cookbook::Legacy::Debugging_BaseClassReplacement β no cap, that name is a mouthful, but we're gonna get through it together π«Ά
β¨ NAME β¨
Moose::Cookbook::Legacy::Debugging_BaseClassReplacement β it's giving "providing an alternate base object class" π
πΈ VERSION πΈ
version 2.4000 β yes, she's at 2.4000, we love a numbered queen β¨
π SYNOPSIS π
package MyApp::Base;
use Moose;
extends 'Moose::Object';
before 'new' => sub { warn "Making a new " . $_[0] };
no Moose;
package MyApp::UseMyBase;
use Moose ();
use Moose::Exporter;
Moose::Exporter->setup_import_methods( also => 'Moose' );
sub init_meta {
shift;
return Moose->init_meta( @_, base_class => 'MyApp::Base' );
}
π¦ DESCRIPTION π¦
WARNING: Replacing the base class entirely, instead of just applying roles to the base class, is strongly discouraged. This recipe exists solely for reference when you run into older code that does this. So likeβ¦ don't do it, bestie, unless you're doing archaeology π
So here's the vibe: a super common extension is wanting to swap in an alternate base class. One way to do that is to make a MyApp::Base and then slap extends 'MyApp::Base' onto every single class in your app. Which isβ¦ so tedious. Exhausting. Not the move π
Instead, you can build yourself a Moose-alike module that sets the base object class to MyApp::Base for you, so you never have to type it again. Iconic behavior, honestly π
Then, instead of writing use Moose, you just write use MyApp::UseMyBase. That's it. That's the whole swap. Slay β¨
In this particular example, our base class just spits out some debugging output every single time a new object gets created β but like, that's the minimum. You can imagine way more interesting things to do with your own base class. Main character base class energy, fr fr π
This all runs on the magic of Moose::Exporter. When we call Moose::Exporter->setup_import_methods( also => 'Moose' ), it builds import and unimport methods for us, for free. The also => 'Moose' part is basically saying "yeah bestie, we want to export everything that Moose does." π
The import method that gets created will call our init_meta method, handing it for_caller => $caller as its arguments. And that $caller is set to the class that actually imported us in the first place. iykyk π§
Wanna know more about its API? Go read the Moose::Exporter docs, they got the details.
π USING MyApp::UseMyBase π
To actually use our new base class, we simply use MyApp::UseMyBase instead of Moose. And boom β we get all the Moose sugar plus our new base class. Best of both worlds, no tradeoffs π
package Foo;
use MyApp::UseMyBase;
has 'size' => ( is => 'rw' );
no MyApp::UseMyBase;
π CONCLUSION π
ngl, this is an awful lot of magic for a simple base class. Like, an unreasonable amount of wizardry for one little class π§
But here's when it actually pays off: you'll often want to combine a metaclass trait with a base class extension, and that's the moment this technique becomes genuinely useful. Periodt β¨
π 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>
absolute legends, every single one of them π«Ά
π 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. π
and that's the whole page bestie!! this was pulled from perl v5.42.0, dated 2025-07-25 β go forth and maybe not replace your base class entirely, mwah π
πβ¨
π¦ 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