{"id":7318,"date":"2022-12-20T19:37:41","date_gmt":"2022-12-20T22:37:41","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/moosemanualmop-man3\/"},"modified":"2022-12-20T19:37:41","modified_gmt":"2022-12-20T22:37:41","slug":"moosemanualmop-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/moosemanualmop-man3\/","title":{"rendered":"Moose::Manual::MOP (man3)"},"content":{"rendered":"<h1 align=\"center\">Moose::Manual::MOP<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#VERSION\">VERSION<\/a><br \/> <a href=\"#INTRODUCTION\">INTRODUCTION<\/a><br \/> <a href=\"#GETTING STARTED\">GETTING STARTED<\/a><br \/> <a href=\"#USING THE METACLASS OBJECT\">USING THE METACLASS OBJECT<\/a><br \/> <a href=\"#ALTERING CLASSES WITH THE MOP\">ALTERING CLASSES WITH THE MOP<\/a><br \/> <a href=\"#GOING FURTHER\">GOING FURTHER<\/a><br \/> <a href=\"#AUTHORS\">AUTHORS<\/a><br \/> <a href=\"#COPYRIGHT AND LICENSE\">COPYRIGHT AND LICENSE<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Moose::Manual::MOP \u2212 The Moose (and Class::MOP) meta API<\/p>\n<h2>VERSION <a name=\"VERSION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">version 2.2014<\/p>\n<h2>INTRODUCTION <a name=\"INTRODUCTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Moose provides a powerful introspection <small>API<\/small> built on top of &#8220;Class::MOP&#8221;. &#8221; <small>MOP&#8221;<\/small> stands for Meta-Object Protocol. In plainer English, a <small>MOP<\/small> is an <small>API<\/small> for performing introspection on classes, attributes, methods, and so on.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In fact, it is &#8220;Class::MOP&#8221; that provides many of Moose\u2019s core features, including attributes, before\/after\/around method modifiers, and immutability. In most cases, Moose takes an existing &#8220;Class::MOP&#8221; 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.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you\u2019re interested in the <small>MOP,<\/small> it\u2019s important to know about &#8220;Class::MOP&#8221; so you know what docs to read. Often, the introspection method that you\u2019re looking for is defined in a &#8220;Class::MOP&#8221; class, rather than Moose itself.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <small>MOP<\/small> provides more than just <i>read-only<\/i> 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 <small>MOP API.<\/small><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you want to write Moose extensions, you\u2019ll need to learn some of the <small>MOP API.<\/small> The introspection methods are also handy if you want to generate docs or inheritance graphs, or do some other runtime reflection.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This document is not a complete reference for the meta <small>API.<\/small> We\u2019re just going to cover some of the highlights, and give you a sense of how it all works. To really understand it, you\u2019ll have to read a lot of other docs, and possibly even dig into the Moose guts a bit.<\/p>\n<h2>GETTING STARTED <a name=\"GETTING STARTED\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The usual entry point to the meta <small>API<\/small> is through a class\u2019s metaclass object, which is a Moose::Meta::Class. This is available by calling the &#8220;meta&#8221; method on a class or object:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">package User; <br \/> use Moose; <br \/> my $meta = __PACKAGE__\u2212>meta;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;meta&#8221; method is added to a class when it uses Moose.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You can also use &#8220;Class::MOP::Class\u2212>initialize($name)&#8221; to get a metaclass object for any class. This is safer than calling &#8220;$class\u2212>meta&#8221; when you\u2019re not sure that the class has a meta method.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;Class::MOP::Class\u2212>initialize&#8221; constructor will return an existing metaclass if one has already been created (via Moose or some other means). If it hasn\u2019t, it will return a new &#8220;Class::MOP::Class&#8221; object. This will work for classes that use Moose, meta <small>API<\/small> classes, and classes which don\u2019t use Moose at all.<\/p>\n<h2>USING THE METACLASS OBJECT <a name=\"USING THE METACLASS OBJECT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The metaclass object can tell you about a class\u2019s attributes, methods, roles, parents, and more. For example, to look at all of the class\u2019s attributes:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">for my $attr ( $meta\u2212>get_all_attributes ) { <br \/> print $attr\u2212>name, &#8220;n&#8221;; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;get_all_attributes&#8221; method is documented in &#8220;Class::MOP::Class&#8221;. For Moose-using classes, it returns a list of Moose::Meta::Attribute objects for attributes defined in the class and its parents.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You can also get a list of methods:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">for my $method ( $meta\u2212>get_all_methods ) { <br \/> print $method\u2212>fully_qualified_name, &#8220;n&#8221;; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Now we\u2019re 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.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">We can look at a class\u2019s parent classes and subclasses:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">for my $class ( $meta\u2212>linearized_isa ) { <br \/> print &#8220;$classn&#8221;; <br \/> } <br \/> for my $subclass ( $meta\u2212>subclasses ) { <br \/> print &#8220;$subclassn&#8221;; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note that both these methods return class <i>names<\/i>, not metaclass objects.<\/p>\n<h2>ALTERING CLASSES WITH THE MOP <a name=\"ALTERING CLASSES WITH THE MOP\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The metaclass object can change the class directly, by adding attributes, methods, etc.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">As an example, we can add a method to a class:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">$meta\u2212>add_method( &#8216;say&#8217; => sub { print @_, &#8220;n&#8221; } );<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Or an attribute:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">$meta\u2212>add_attribute( &#8216;size&#8217; => ( is => &#8216;rw&#8217;, isa => &#8216;Int&#8217; ) );<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Obviously, this is much more cumbersome than using Perl syntax or Moose sugar for defining methods and attributes, but this <small>API<\/small> allows for very powerful extensions.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You might remember that we\u2019ve 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.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You can make a class mutable again simply by calling &#8220;$meta\u2212>make_mutable&#8221;. Once you\u2019re done changing it, you can restore immutability by calling &#8220;$meta\u2212>make_immutable&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">However, the most common use for this part of the meta <small>API<\/small> is as part of Moose extensions. These extensions should assume that they are being run before you make a class immutable.<\/p>\n<h2>GOING FURTHER <a name=\"GOING FURTHER\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">If you\u2019re interested in extending Moose, we recommend reading all of the &#8220;Meta&#8221; and &#8220;Extending&#8221; recipes in the Moose::Cookbook. Those recipes show various practical applications of the <small>MOP.<\/small><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you\u2019d 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\u2019ll probably also need to read various <small>API<\/small> docs, including the docs for the various &#8220;Moose::Meta::*&#8221; and &#8220;Class::MOP::*&#8221; classes.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Finally, we welcome questions on the Moose mailing list and <small>IRC.<\/small> Information on the mailing list, <small>IRC,<\/small> and more references can be found in the Moose.pm docs.<\/p>\n<h2>AUTHORS <a name=\"AUTHORS\"><\/a> <\/h2>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p style=\"margin-top: 1em\">Stevan Little <stevan@cpan.org><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>Dave Rolsky <autarch@urth.org><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>Jesse Luehrs <doy@cpan.org><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>Shawn M Moore <sartak@cpan.org><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>\u00d7\u00d7\u00d7\u00d7 \u00d7\u00a7\u00d7\u00d7\u2019\u00d7\u00d7 (Yuval Kogman) <nothingmuch@woobling.org><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>Karen Etheridge <ether@cpan.org><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>Florian Ragwitz <rafl@debian.org><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>Hans Dieter Pearcey <hdp@cpan.org><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>Chris Prather <chris@prather.org><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>Matt S Trout <mstrout@cpan.org><\/p>\n<\/td>\n<\/tr>\n<\/table>\n<h2>COPYRIGHT AND LICENSE <a name=\"COPYRIGHT AND LICENSE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This software is copyright (c) 2006 by Infinity Interactive, Inc.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This is free software; you can redistribute it and\/or modify it under the same terms as the Perl 5 programming language system itself.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  Moose::Manual::MOP \u2212 The Moose (and Class::MOP) meta API <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3007,3637],"class_list":["post-7318","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-man3","tag-moosemanualmop"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=7318"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7318\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=7318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=7318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=7318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}