{"id":7420,"date":"2022-12-20T19:38:01","date_gmt":"2022-12-20T22:38:01","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/moosecookbookmetatable_metaclasstrait-man3\/"},"modified":"2022-12-20T19:38:01","modified_gmt":"2022-12-20T22:38:01","slug":"moosecookbookmetatable_metaclasstrait-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/moosecookbookmetatable_metaclasstrait-man3\/","title":{"rendered":"Moose::Cookbook::Meta::Table_MetaclassTrait (man3)"},"content":{"rendered":"<h1 align=\"center\">Moose::Cookbook::Meta::Table_MetaclassTrait<\/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=\"#THE METACLASS TRAIT\">THE METACLASS TRAIT<\/a><br \/> <a href=\"#RECIPE CAVEAT\">RECIPE CAVEAT<\/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::Table_MetaclassTrait \u2212 Adding a &#8220;table&#8221; attribute as a metaclass trait<\/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\"># in lib\/MyApp\/Meta\/Class\/Trait\/HasTable.pm <br \/> package MyApp::Meta::Class::Trait::HasTable; <br \/> use Moose::Role; <br \/> Moose::Util::meta_class_alias(&#8216;HasTable&#8217;); <br \/> has table => ( <br \/> is => &#8216;rw&#8217;, <br \/> isa => &#8216;Str&#8217;, <br \/> ); <br \/> # in lib\/MyApp\/User.pm <br \/> package MyApp::User; <br \/> use Moose \u2212traits => &#8216;HasTable&#8217;; <br \/> __PACKAGE__\u2212>meta\u2212>table(&#8216;User&#8217;);<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">In this recipe, we\u2019ll create a class metaclass trait which has a &#8220;table&#8221; attribute. This trait is for classes associated with a <small>DBMS<\/small> table, as one might do for an <small>ORM.<\/small><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In this example, the table name is just a string, but in a real <small>ORM<\/small> the table might be an object describing the table.<\/p>\n<h2>THE METACLASS TRAIT <a name=\"THE METACLASS TRAIT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This really is as simple as the recipe &#8221; <small>SYNOPSIS&#8221;<\/small> shows. The trick is getting your classes to use this metaclass, and providing some sort of sugar for declaring the table. This is covered in Moose::Cookbook::Extending::Debugging_BaseClassRole, which shows how to make a module like &#8220;Moose.pm&#8221; itself, with sugar like &#8220;has_table()&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Using this Metaclass Trait in Practice<\/b> <br \/> Accessing this new &#8220;table&#8221; attribute is quite simple. Given a class named &#8220;MyApp::User&#8221;, we could simply write the following:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $table = MyApp::User\u2212>meta\u2212>table;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">As long as &#8220;MyApp::User&#8221; has arranged to apply the &#8220;MyApp::Meta::Class::Trait::HasTable&#8221; to its metaclass, this method call just works. If we want to be more careful, we can check that the class metaclass object has a &#8220;table&#8221; method:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">$table = MyApp::User\u2212>meta\u2212>table <br \/> if MyApp::User\u2212>meta\u2212>can(&#8216;table&#8217;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In theory, this is not entirely correct, since the metaclass might be getting its &#8220;table&#8221; method from a <i>different<\/i> trait. In practice, you are unlikely to encounter this sort of problem.<\/p>\n<h2>RECIPE CAVEAT <a name=\"RECIPE CAVEAT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This recipe doesn\u2019t work when you paste it all into a single file. This is because the &#8220;use Moose \u2212traits => &#8216;HasTable&#8217;;&#8221; line ends up being executed before the &#8220;table&#8221; attribute is defined.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When the two packages are separate files, this just works.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Moose::Cookbook::Meta::Labeled_AttributeTrait \u2212 Labels implemented via attribute traits =pod<\/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::Table_MetaclassTrait \u2212 Adding a &#8220;table&#8221; attribute as a metaclass trait <\/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,3732],"class_list":["post-7420","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-man3","tag-moosecookbookmetatable_metaclasstrait"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7420","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=7420"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7420\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=7420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=7420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=7420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}