{
    "mode": "perldoc",
    "parameter": "Moose::Cookbook::Meta::Table_MetaclassTrait",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3ACookbook%3A%3AMeta%3A%3ATable_MetaclassTrait/json",
    "generated": "2026-11-27T22:05:16Z",
    "synopsis": "# in lib/MyApp/Meta/Class/Trait/HasTable.pm\npackage MyApp::Meta::Class::Trait::HasTable;\nuse Moose::Role;\nMoose::Util::metaclassalias('HasTable');\nhas table => (\nis  => 'rw',\nisa => 'Str',\n);\n# in lib/MyApp/User.pm\npackage MyApp::User;\nuse Moose -traits => 'HasTable';\nPACKAGE->meta->table('User');",
    "sections": {
        "NAME": {
            "content": "Moose::Cookbook::Meta::TableMetaclassTrait - Adding a \"table\" attribute as a metaclass trait\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 2.2207\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "# in lib/MyApp/Meta/Class/Trait/HasTable.pm\npackage MyApp::Meta::Class::Trait::HasTable;\nuse Moose::Role;\nMoose::Util::metaclassalias('HasTable');\n\nhas table => (\nis  => 'rw',\nisa => 'Str',\n);\n\n# in lib/MyApp/User.pm\npackage MyApp::User;\nuse Moose -traits => 'HasTable';\n\nPACKAGE->meta->table('User');\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "In this recipe, we'll create a class metaclass trait which has a \"table\" attribute. This trait\nis for classes associated with a DBMS table, as one might do for an ORM.\n\nIn this example, the table name is just a string, but in a real ORM the table might be an object\ndescribing the table.\n",
            "subsections": []
        },
        "THE METACLASS TRAIT": {
            "content": "This really is as simple as the recipe \"SYNOPSIS\" shows. The trick is getting your classes to\nuse this metaclass, and providing some sort of sugar for declaring the table. This is covered in\nMoose::Cookbook::Extending::DebuggingBaseClassRole, which shows how to make a module like\n\"Moose.pm\" itself, with sugar like hastable().\n",
            "subsections": [
                {
                    "name": "Using this Metaclass Trait in Practice",
                    "content": "Accessing this new \"table\" attribute is quite simple. Given a class named \"MyApp::User\", we\ncould simply write the following:\n\nmy $table = MyApp::User->meta->table;\n\nAs long as \"MyApp::User\" has arranged to apply the \"MyApp::Meta::Class::Trait::HasTable\" to its\nmetaclass, this method call just works. If we want to be more careful, we can check that the\nclass metaclass object has a \"table\" method:\n\n$table = MyApp::User->meta->table\nif MyApp::User->meta->can('table');\n\nIn theory, this is not entirely correct, since the metaclass might be getting its \"table\" method\nfrom a *different* trait. In practice, you are unlikely to encounter this sort of problem.\n"
                }
            ]
        },
        "RECIPE CAVEAT": {
            "content": "This recipe doesn't work when you paste it all into a single file. This is because the \"use\nMoose -traits => 'HasTable';\" line ends up being executed before the \"table\" attribute is\ndefined.\n\nWhen the two packages are separate files, this just works.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Moose::Cookbook::Meta::LabeledAttributeTrait - Labels implemented via attribute traits =pod\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "*   Stevan Little <stevan@cpan.org>\n\n*   Dave Rolsky <autarch@urth.org>\n\n*   Jesse Luehrs <doy@cpan.org>\n\n*   Shawn M Moore <sartak@cpan.org>\n\n*   יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>\n\n*   Karen Etheridge <ether@cpan.org>\n\n*   Florian Ragwitz <rafl@debian.org>\n\n*   Hans Dieter Pearcey <hdp@cpan.org>\n\n*   Chris Prather <chris@prather.org>\n\n*   Matt S Trout <mstrout@cpan.org>\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "This software is copyright (c) 2006 by Infinity Interactive, Inc.\n\nThis is free software; you can redistribute it and/or modify it under the same terms as the Perl\n5 programming language system itself.\n",
            "subsections": []
        }
    },
    "summary": "Moose::Cookbook::Meta::TableMetaclassTrait - Adding a \"table\" attribute as a metaclass trait",
    "flags": [],
    "examples": [],
    "see_also": []
}