-
-
07 Nov 2021 03:41:58 UTC
- Distribution: Moose
- Source (raw)
- Browse (raw)
- Changes
- Homepage
- How to Contribute
- Repository
- Issues (71)
- Testers (2810 / 3 / 1)
- Kwalitee
Bus factor: 3- 92.11% Coverage
- License: perl_5
- Perl: v5.8.3
- Activity
24 month- Tools
- Download (881.54KB)
- MetaCPAN Explorer
- Permissions
- Subscribe to distribution
- Permalinks
- This version
- Latest version
and 142 contributors-
Stevan Little
-
Dave Rolsky
-
Jesse Luehrs
-
Shawn M Moore
-
יובל קוג'מן (Yuval Kogman)
-
Florian Ragwitz
-
Hans Dieter Pearcey
-
Chris Prather
-
Matt S Trout
-
Upasana Shukla
-
Graham Knop
-
Tomas Doran
-
Ricardo Signes
-
Guillermo Roditi
-
John Napiorkowski
-
Aankhen
-
Todd Hepler
-
Jonathan Rockway
-
Gerda Shank
-
Perlover
-
Shlomi Fish
-
Brad Bowman
-
Justin Hunter
-
Kent Fredric
-
Paul Driver
-
Anders Nor Berle
-
Brian Manning
-
gfx
-
Jay Hannah
-
Lars Dɪᴇᴄᴋᴏᴡ 迪拉斯
-
Leon Brocard
-
Olivier Mengué
-
Rafael Kitover
-
Christian Hansen
-
Cory Watson
-
Dagfinn Ilmari Mannsåker
-
Paul Jamieson Fenwick
-
Robert Buels
-
Dan Dascalescu
-
Marcel Grünauer
-
Scott McWhirter
-
Ævar Arnfjörð Bjarmason
-
Daisuke Maki (lestrrat)
-
Dylan William Hardison
-
Patrick Donelan
-
Stefan O'Rear
-
Tokuhiro Matsuno
-
Ash Berlin
-
Chris Weyl
-
Eric Wilhelm
-
Jess Robinson
-
Marc Mims
-
Marcus Ramberg
-
Mark Allen
-
Mateu X Hunter
-
matthof
-
Robert 'phaylon' Sedlacek
-
Zachary Lome
-
Aran Clary Deltac
-
Chip
-
Christopher J. Madsen
-
Curtis Jewell
-
Evan Carroll
-
Mark A. Stratman
-
Mark Fowler
-
Matthew Horsfall
-
mauke
-
Michael LaGrasta
-
Michael Rykov
-
Mike Whitaker
-
Moritz Onken
-
Nelo Onyiah
-
Nick Perez
-
Robert Boone
-
Robin V
-
rodrigolive
-
shelling
-
Thomas Sibley
-
Tom Hukins
-
Wallace Reis
-
Aaron Cohen
-
Adam J. Foxson
-
Adam Kennedy
-
Andy Jack
-
Anirvan Chatterjee
-
Ansgar Burchardt
-
A. Sinan Unur
-
Ben Hutton
-
Brendan Byrd
-
Chad Granum
-
Chankey Pathak
-
Chia-liang Kao
-
Christian Walde (Mithaldu)
-
chromatic
-
Dann
-
Dave Romano
-
David Leadbeater
-
David Steinbrunner
-
dmaestro
-
E. Choroba
-
franck cuny
-
Frew Schmidt
-
gregor herrmann
-
hakim
-
Henry Van Styn
-
James Marca
-
Jason May
-
Jay Allen
-
Jay Kuri
-
Jeff Bisbee
-
Jens Berthold
-
Jesse Vincent
-
joel
-
John Douglas Porter
-
John Goulah
-
Justin DeVuyst
-
Kang-min Liu
-
Leon Timmermans
-
Mark O Grady
-
Matt Kraai
-
Michael Schout
-
Nathan Gray
-
Olaf Alders
-
Olof Johansson
-
Paul Cochrane
-
Paweł Murias
-
Pedro Melo
-
Peter Shangov
-
Philippe Bruhat (BooK)
-
Philipp Gortan
-
Phillip Smith
-
Piotr Roszatycki
-
pktm
-
rouzier
-
Sam Vilain
-
sherrardb
-
Simon Reinhardt
-
sue spence
-
Tuomas Jormola
-
wickline
-
Yanick Champoux
-
Zoffix Znet
- Dependencies
- Carp
- Class::Load
- Class::Load::XS
- Data::OptList
- Devel::GlobalDestruction
- Devel::OverloadInfo
- Devel::StackTrace
- Dist::CheckConflicts
- Eval::Closure
- List::Util
- MRO::Compat
- Module::Runtime
- Module::Runtime::Conflicts
- Package::DeprecationManager
- Package::Stash
- Package::Stash::XS
- Params::Util
- Scalar::Util
- Sub::Exporter
- Sub::Util
- Try::Tiny
- parent
- strict
- warnings
- Reverse dependencies
- CPAN Testers List
- Dependency graph
- NAME
- VERSION
- INTRODUCTION
- GETTING STARTED
- USING THE METACLASS OBJECT
- ALTERING CLASSES WITH THE MOP
- GOING FURTHER
- AUTHORS
- COPYRIGHT AND LICENSE
NAME
Moose::Manual::MOP - The Moose (and Class::MOP) meta API
VERSION
version 2.2201
INTRODUCTION
Moose provides a powerful introspection API built on top of
Class::MOP
. "MOP" stands for Meta-Object Protocol. In plainer English, a MOP is an API for performing introspection on classes, attributes, methods, and so on.In 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 existingClass::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 MOP, it's important to know about
Class::MOP
so you know what docs to read. Often, the introspection method that you're looking for is defined in aClass::MOP
class, rather than Moose itself.The MOP provides more than just read-only introspection. 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.
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.
This document is not a complete reference for the meta API. We're just going to cover some of the highlights, and give 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.
GETTING STARTED
The usual entry point to the meta API is through a class's metaclass object, which is a Moose::Meta::Class. This is available by calling the
meta
method on a class or object: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 newClass::MOP::Class
object. This will work for classes that use Moose, meta API classes, and classes which don't use Moose at all.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 inClass::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 API allows for very powerful extensions.
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 API is as part of Moose extensions. These extensions should assume that they are being run before you make a class immutable.
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.
If 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 API docs, including the docs for the various
Moose::Meta::*
andClass::MOP::*
classes.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.
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>
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.
Module Install Instructions
To install Moose, copy and paste the appropriate command in to your terminal.
cpanm Moose
perl -MCPAN -e shell install Moose
For more information on module installation, please visit the detailed CPAN module installation guide.