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 like Moose.pm and 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>

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