Moose::...assTrait(3)

πŸ’– NAME πŸ’–

Moose::Cookbook::Meta::Table_MetaclassTrait β€” adding a "table" attribute as a metaclass trait, bestie πŸ’…

This is a page from the Moose::Cookbook series, living in the User Contributed Perl Documentation section (that's the 3 in the manual name, iykyk). Grab a snack, we're building metaclass traits πŸ“

✨ VERSION ✨

version 2.4000


🌈 SYNOPSIS 🌈

  # in lib/MyApp/Meta/Class/Trait/HasTable.pm
  package MyApp::Meta::Class::Trait::HasTable;
  use Moose::Role;
  Moose::Util::meta_class_alias('HasTable');

  has table => (
      is  => 'rw',
      isa => 'Str',
  );

  # in lib/MyApp/User.pm
  package MyApp::User;
  use Moose -traits => 'HasTable';

  __PACKAGE__->meta->table('User');

πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so in this recipe we're making a class metaclass trait that has a "table" attribute. It's for classes that map to a DBMS table β€” the kind of thing you'd do when you're building an ORM. Main character energy for databases πŸ‘‘

In this example the table name is just a string, but fr fr, in a real ORM that table could totally be an object describing the table instead. No cap.


πŸ‘‘ THE METACLASS TRAIT πŸ‘‘

It literally is as simple as the recipe in SYNOPSIS shows. The trick is getting your classes to actually use this metaclass, plus giving yourself some sugar for declaring the table. That whole situation is covered over in Moose::Cookbook::Extending::Debugging_BaseClassRole, which shows how to make a module like "Moose.pm" itself, with sugar like has_table(). Slay.

πŸŽ€ Using this Metaclass Trait in Practice πŸŽ€

Accessing this new "table" attribute is so easy. Say you've got a class named "MyApp::User" β€” you could simply write:

  my $table = MyApp::User->meta->table;

As long as "MyApp::User" has arranged to apply the "MyApp::Meta::Class::Trait::HasTable" to its metaclass, this method call just works. Periodt. πŸ’…

But if you wanna be more careful (and honestly, valid), you can check that the class metaclass object actually has a "table" method:

  $table = MyApp::User->meta->table
      if MyApp::User->meta->can('table');

In theory this is not entirely correct, because the metaclass might be getting its "table" method from a different trait. In practice though, you're unlikely to run into this sort of problem. So like… lowkey fine πŸ™ˆ


πŸ’€ RECIPE CAVEAT πŸ’€

This recipe does not work when you paste it all into a single file. Why? Because the "use Moose -traits => 'HasTable';" line ends up being executed before the "table" attribute is defined. Order matters, bestie 😭

When the two packages are separate files, it just works. Simple as that.


🌸 SEE ALSO 🌸

  • Moose::Cookbook::Meta::Labeled_AttributeTrait - Labels implemented via attribute traits =pod

🫢 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>

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. ✨


Okay that's the whole page, mwah πŸ’‹ this was perl v5.42.0, dated 2025-07-25, and it's the Moose::...assTrait(3) manpage from your friendly neighbourhood User Contributed Perl Documentation. Go build something iconic πŸ§šβ€β™€οΈπŸŒˆβ­

πŸ“¦ where this page comes from