Moose::Manual::MOP(3)

✨ NAME ✨

Moose::Manual::MOP β€” the Moose (and Class::MOP) meta API, bestie πŸ’–

(also this is Moose::Manual::MOP(3), from the User Contributed Perl Documentation, iykyk)

version 2.4000 β€” that's the VERSION, she's cute, she's current ✨


πŸ¦‹ INTRODUCTION πŸ¦‹

okay so Moose gives you a powerful introspection API and it's built on top of Class::MOP. And "MOP"? It stands for Meta-Object Protocol. In plainer English (no gatekeeping here), a MOP is just an API for doing introspection on classes, attributes, methods, and so on. literally just vibes-checking your own code 🌸

fr fr it is Class::MOP that provides so many of Moose's core features β€” attributes, before/after/around method modifiers, and immutability. In most cases, Moose takes an existing Class::MOP class and subclasses it to add extra features. Moose also adds some entirely new features of its own, like roles, the augment modifier, and types. main character energy honestly πŸ‘‘

If you're interested in the MOP, it's important to know about Class::MOP so you know what docs to read. Often, the introspection method you're looking for is defined in a Class::MOP class, not in Moose itself. so like, don't go looking in the wrong place and get mad at Moose πŸ’…

And the MOP is not just read-only introspection, bestie. It also lets you add attributes and methods, apply roles, and much more. In fact, all of the declarative Moose sugar is simply a thin layer on top of the MOP API. the sugar is the garnish, the MOP is the meal πŸ“

If you want to write Moose extensions, you'll need to learn some of the MOP API. The introspection methods are also handy if you want to generate docs or inheritance graphs, or do some other runtime reflection. obsessed, honestly 🌈

This document is not a complete reference for the meta API. We're just covering some of the highlights and giving you a sense of how it all works. To really understand it, you'll have to read a lot of other docs, and possibly even dig into the Moose guts a bit. 😭 (yes, the guts. we said what we said.)

🌟 GETTING STARTED 🌟

The usual entry point to the meta API is through a class's metaclass object, which is a Moose::Meta::Class. You get it by calling the meta method on a class or an object:

package User;

use Moose;

my $meta = __PACKAGE__->meta;

The meta method is added to a class when it uses Moose. cute little gift with purchase πŸŽ€

You can also use Class::MOP::Class->initialize($name) to get a metaclass object for any class. This is safer than calling $class->meta when you're not sure that the class has a meta method. (like, don't just assume, that's rude and also will crash πŸ’€)

The Class::MOP::Class->initialize constructor will return an existing metaclass if one has already been created (via Moose or some other means). If it hasn't, it will return a new Class::MOP::Class object. This will work for classes that use Moose, meta API classes, and classes which don't use Moose at all. equal opportunity metaclassing 🫢


πŸ’œ USING THE METACLASS OBJECT πŸ’œ

The metaclass object can tell you about a class's attributes, methods, roles, parents, and more. For example, to look at all of the class's attributes:

for my $attr ( $meta->get_all_attributes ) {
    print $attr->name, "\n";
}

The get_all_attributes method is documented in Class::MOP::Class. For Moose-using classes, it returns a list of Moose::Meta::Attribute objects for attributes defined in the class and its parents. yes, the whole family tree 🌳

You can also get a list of methods:

for my $method ( $meta->get_all_methods ) {
    print $method->fully_qualified_name, "\n";
}

Now we're looping over a list of Moose::Meta::Method objects. Note that some of these objects may actually be a subclass of Moose::Meta::Method, because Moose uses different classes to represent wrapped methods, delegation methods, constructors, etc. it's giving variety ✨

We can look at a class's parent classes and subclasses:

for my $class ( $meta->linearized_isa ) {
    print "$class\n";
}

for my $subclass ( $meta->subclasses ) {
    print "$subclass\n";
}
Heads up: both these methods return class names, not metaclass objects. don't @ me, that's just how it is πŸ’…

πŸ”₯ ALTERING CLASSES WITH THE MOP πŸ”₯

The metaclass object can change the class directly β€” adding attributes, methods, etc. it's not just a reader, she's a whole author ✍️

As an example, we can add a method to a class:

$meta->add_method( 'say' => sub { print @_, "\n" } );

Or an attribute:

$meta->add_attribute( 'size' => ( is => 'rw', isa  => 'Int' ) );

Obviously, this is much more cumbersome than using Perl syntax or Moose sugar for defining methods and attributes, but this API allows for very powerful extensions. like, it's ugly but it's powerful, and that's the trade 🫠

You might remember that we've talked about making classes immutable elsewhere in the manual. This is a good practice. However, once a class is immutable, calling any of these update methods will throw an exception. πŸ”₯ not a vibe check, an actual exception.

You can make a class mutable again simply by calling $meta->make_mutable. Once you're done changing it, you can restore immutability by calling $meta->make_immutable. thaw her out, do your thing, freeze her again 🧊

However, the most common use for this part of the meta API is as part of Moose extensions. These extensions should assume that they are being run before you make a class immutable. write that down πŸ“


🌈 GOING FURTHER 🌈

If you're interested in extending Moose, we recommend reading all of the "Meta" and "Extending" recipes in the Moose::Cookbook. Those recipes show various practical applications of the MOP. study the canon, bestie πŸ“š

If you'd like to write your own extensions, one of the best ways to learn more is to look at other similar extensions to see how they work. You'll probably also need to read various API docs, including the docs for the various Moose::Meta::* and Class::MOP::* classes. read the source, it's free πŸ’–

Finally, we welcome questions on the Moose mailing list and IRC. Information on the mailing list, IRC, and more references can be found in the Moose.pm docs. come hang out, we don't bite 🫢


πŸ‘‘ 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>

the whole squad, iconic, no notes ⭐


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 perl v5.42.0, dated 2025-07-25, from Moose::Manual::MOP(3). go forth and introspect responsibly πŸ’…βœ¨πŸŒΈ

πŸ“¦ where this page comes from