{"id":7164,"date":"2022-12-20T19:35:41","date_gmt":"2022-12-20T22:35:41","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/moosecookbookmetaglobref_instancemetaclass-man3\/"},"modified":"2022-12-20T19:35:41","modified_gmt":"2022-12-20T22:35:41","slug":"moosecookbookmetaglobref_instancemetaclass-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/moosecookbookmetaglobref_instancemetaclass-man3\/","title":{"rendered":"Moose::Cookbook::Meta::GlobRef_InstanceMetaclass (man3)"},"content":{"rendered":"<h1 align=\"center\">Moose::Cookbook::Meta::GlobRef_InstanceMetaclass<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#VERSION\">VERSION<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#CONCLUSION\">CONCLUSION<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/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::Cookbook::Meta::GlobRef_InstanceMetaclass \u2212 Creating a glob reference meta\u2212instance class<\/p>\n<h2>VERSION <a name=\"VERSION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">version 2.2014<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">package My::Meta::Instance; <br \/> use Scalar::Util qw( weaken ); <br \/> use Symbol qw( gensym ); <br \/> use Moose::Role; <br \/> sub create_instance { <br \/> my $self = shift; <br \/> my $sym = gensym(); <br \/> bless $sym, $self\u2212>_class_name; <br \/> } <br \/> sub clone_instance { <br \/> my ( $self, $instance ) = @_; <br \/> my $new_sym = gensym(); <br \/> %{*$new_sym} = %{*$instance}; <br \/> bless $new_sym, $self\u2212>_class_name; <br \/> } <br \/> sub get_slot_value { <br \/> my ( $self, $instance, $slot_name ) = @_; <br \/> return *$instance\u2212>{$slot_name}; <br \/> } <br \/> sub set_slot_value { <br \/> my ( $self, $instance, $slot_name, $value ) = @_; <br \/> *$instance\u2212>{$slot_name} = $value; <br \/> } <br \/> sub deinitialize_slot { <br \/> my ( $self, $instance, $slot_name ) = @_; <br \/> delete *$instance\u2212>{$slot_name}; <br \/> } <br \/> sub is_slot_initialized { <br \/> my ( $self, $instance, $slot_name ) = @_; <br \/> exists *$instance\u2212>{$slot_name}; <br \/> } <br \/> sub weaken_slot_value { <br \/> my ( $self, $instance, $slot_name ) = @_; <br \/> weaken *$instance\u2212>{$slot_name}; <br \/> } <br \/> sub inline_create_instance { <br \/> my ( $self, $class_variable ) = @_; <br \/> return &#8216;do { my $sym = Symbol::gensym(); bless $sym, &#8216; . $class_variable . &#8216; }&#8217;; <br \/> } <br \/> sub inline_slot_access { <br \/> my ( $self, $instance, $slot_name ) = @_; <br \/> return &#8216;*{&#8216; . $instance . &#8216;}\u2212>{&#8216; . $slot_name . &#8216;}&#8217;; <br \/> } <br \/> package MyApp::User; <br \/> use Moose; <br \/> Moose::Util::MetaRole::apply_metaroles( <br \/> for => __PACKAGE__, <br \/> class_metaroles => { <br \/> instance => [&#8216;My::Meta::Instance&#8217;], <br \/> }, <br \/> ); <br \/> has &#8216;name&#8217; => ( <br \/> is => &#8216;rw&#8217;, <br \/> isa => &#8216;Str&#8217;, <br \/> ); <br \/> has &#8216;email&#8217; => ( <br \/> is => &#8216;rw&#8217;, <br \/> isa => &#8216;Str&#8217;, <br \/> );<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This recipe shows how to build your own meta-instance. The meta instance is the metaclass that creates object instances and helps manages access to attribute slots.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In this example, we\u2019re creating a meta-instance that is based on a glob reference rather than a hash reference. This example is largely based on the Piotr Roszatycki\u2019s MooseX::GlobRef module.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Our extension is a role which will be applied to Moose::Meta::Instance, which creates hash reference based objects. We need to override all the methods which make assumptions about the object\u2019s data structure.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The first method we override is &#8220;create_instance&#8221;:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">sub create_instance { <br \/> my $self = shift; <br \/> my $sym = gensym(); <br \/> bless $sym, $self\u2212>_class_name; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This returns an glob reference which has been blessed into our meta-instance\u2019s associated class.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">We also override &#8220;clone_instance&#8221; to create a new array reference:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">sub clone_instance { <br \/> my ( $self, $instance ) = @_; <br \/> my $new_sym = gensym(); <br \/> %{*$new_sym} = %{*$instance}; <br \/> bless $new_sym, $self\u2212>_class_name; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">After that, we have a series of methods which mediate access to the object\u2019s slots (attributes are stored in &#8220;slots&#8221;). In the default instance class, these expect the object to be a hash reference, but we need to change this to expect a glob reference instead.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">sub get_slot_value { <br \/> my ( $self, $instance, $slot_name ) = @_; <br \/> *$instance\u2212>{$slot_name}; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This level of indirection probably makes our instance class <i>slower<\/i> than the default. However, when attribute access is inlined, this lookup will be cached:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">sub inline_slot_access { <br \/> my ( $self, $instance, $slot_name ) = @_; <br \/> return &#8216;*{&#8216; . $instance . &#8216;}\u2212>{&#8216; . $slot_name . &#8216;}&#8217;; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The code snippet that the &#8220;inline_slot_access&#8221; method returns will get &#8220;eval&#8221;\u2019d once per attribute.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Finally, we use this meta-instance in our &#8220;MyApp::User&#8221; class:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Moose::Util::MetaRole::apply_metaroles( <br \/> for => __PACKAGE__, <br \/> class_metaroles => { <br \/> instance => [&#8216;My::Meta::Instance&#8217;], <br \/> }, <br \/> );<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">We actually don\u2019t recommend the use of Moose::Util::MetaRole directly in your class in most cases. Typically, this would be provided by a Moose::Exporter\u2212based module which handles applying the role for you.<\/p>\n<h2>CONCLUSION <a name=\"CONCLUSION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This recipe shows how to create your own meta-instance class. It\u2019s unlikely that you\u2019ll need to do this yourself, but it\u2019s interesting to take a peek at how Moose works under the hood.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">There are a few meta-instance class extensions on <small>CPAN:<\/small><\/p>\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>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"26%\">\n<p>MooseX::Singleton<\/p>\n<\/td>\n<td width=\"57%\"> <\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\">This module extends the instance class in order to ensure that the object is a singleton. The instance it uses is still a blessed hash reference.<\/p>\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=\"23%\">\n<p style=\"margin-top: 1em\">MooseX::GlobRef<\/p>\n<\/td>\n<td width=\"60%\"> <\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\">This module makes the instance a blessed glob reference. This lets you use a handle as an object instance.<\/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::Cookbook::Meta::GlobRef_InstanceMetaclass \u2212 Creating a glob reference meta\u2212instance class <\/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,3499],"class_list":["post-7164","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-man3","tag-moosecookbookmetaglobref_instancemetaclass"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7164","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=7164"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7164\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=7164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=7164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=7164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}