Moose::...etaclass(3)
π NAME π
Moose::Cookbook::Legacy::Table_ClassMetaclass β the one where we add a "table" attribute to the metaclass. Yes bestie, to the metaclass itself. It's giving very 2006, and we're about to talk about why. π«Ά
This page lives in the User Contributed Perl Documentation collection, so consider this your lil' group-chat briefing on Moose::...etaclass(3).
β¨ VERSION β¨
version 2.4000 β she's a classic, okay? π
πΈ SYNOPSIS πΈ
package MyApp::Meta::Class;
use Moose;
extends 'Moose::Meta::Class';
has table => (
is => 'rw',
isa => 'Str',
);
That's literally it. That's the whole recipe. Slay. π
π¦ DESCRIPTION π¦
WARNING: Subclassing metaclasses (as opposed to providing metaclass traits) is strongly discouraged. This recipe is provided solely for reference when encountering older code that does this. Not me warning you twice. π
So in this recipe we're gonna create a brand-new metaclass that has a "table" attribute. Why? Because this metaclass is for classes that map onto a DBMS table β the kind of thing you'd do for an ORM.
In this example the table name is just a string, super lowkey. But in a real ORM the table might be an object describing the table. Fancy. β¨
π THE METACLASS π
Genuinely, it really is as simple as the recipe up in "SYNOPSIS" shows. No gatekeeping, no hidden steps. The actual trick is getting your classes to use this metaclass in the first place, plus providing some kind of sugar for declaring the table. And that whole saga is covered in Moose::Cookbook::Extending::Recipe2, which shows you how to build a module like "Moose.pm" itself, sugar included β stuff like has_table(). π
π§ Using this Metaclass in Practice
Accessing this new "table" attribute? So easy, bestie. Say you've got a class named "MyApp::User" β you can just write:
my $table = MyApp::User->meta->table;
As long as "MyApp::User" has arranged to use "MyApp::Meta::Class" as its metaclass, that method call just works. Periodt. π
But if you wanna be more careful β vibe check your metaclass before you touch it β you can check the metaclass's class first:
$table = MyApp::User->meta->table
if MyApp::User->meta->isa('MyApp::Meta::Class');
Safety first, always. π
π CONCLUSION π
Creating a custom metaclass is trivial. Using it? A little harder, ngl β and that's covered in other recipes. We're also gonna talk about applying traits to a class metaclass, which is the more flexible and cooperative way to implement all of this. Main character energy, honestly. β
π SEE ALSO π
Moose::Cookbook::Meta::Recipe5β the"table"attribute implemented as a metaclass trait (the glow-up version).Moose::Cookbook::Extending::Recipe2β acting likeMoose.pmand providing sugar Moose-style.
πΈ AUTHORS πΈ
The absolute icons who made this happen:
- 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.
That's a wrap, bestie β this was Moose::...etaclass(3) from perl v5.42.0, dated 2025-07-25, filed under User Contributed Perl Documentation. Go forth and maybe don't subclass your metaclass. πβ¨
π¦ 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