Moose::Manual::MOP (3)
Leading comments
Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) Standard preamble: ========================================================================
NAME
Moose::Manual::MOP - The Moose (and Class::MOP) meta APIVERSION
version 2.1807INTRODUCTION
Moose provides a powerful introspectionIn fact, it is "Class::MOP" that provides many of Moose's core features, including attributes, before/after/around method modifiers, and immutability. In most cases, Moose takes an existing "Class::MOP" class and subclasses it to add additional features. Moose also adds some entirely new features of its own, such as roles, the augment modifier, and types.
If you're interested in the
The
If you want to write Moose extensions, you'll need to learn some of the
This document is not a complete reference for the meta
GETTING STARTED
The usual entry point to the meta
package User; use Moose; my $meta = __PACKAGE__->meta;
The "meta" method is added to a class when it uses Moose.
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.
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
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.
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, as Moose uses different classes to represent wrapped methods, delegation methods, constructors, etc.
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"; }
Note that both these methods return class names, not metaclass objects.
ALTERING CLASSES WITH THE MOP
The metaclass object can change the class directly, by adding attributes, methods, etc.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
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.
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".
However, the most common use for this part of the meta
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 theIf you'd like to write your own extensions, one of the best ways to learn more about this is to look at other similar extensions to see how they work. You'll probably also need to read various
Finally, we welcome questions on the Moose mailing list and
AUTHORS
- *
- Stevan Little <stevan.little@iinteractive.com>
- *
- Dave Rolsky <autarch@urth.org>
- *
- Jesse Luehrs <doy@tozt.net>
- *
- Shawn M Moore <code@sartak.org>
- *
- יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
- *
- Karen Etheridge <ether@cpan.org>
- *
- Florian Ragwitz <rafl@debian.org>
- *
- Hans Dieter Pearcey <hdp@weftsoar.net>
- *
- Chris Prather <chris@prather.org>
- *
- Matt S Trout <mst@shadowcat.co.uk>
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.