{
    "mode": "perldoc",
    "parameter": "Moose::Cookbook::Meta::Labeled_AttributeTrait",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3ACookbook%3A%3AMeta%3A%3ALabeled_AttributeTrait/json",
    "generated": "2026-11-22T02:48:50Z",
    "synopsis": "package MyApp::Meta::Attribute::Trait::Labeled;\nuse Moose::Role;\nMoose::Util::metaattributealias('Labeled');\nhas label => (\nis        => 'rw',\nisa       => 'Str',\npredicate => 'haslabel',\n);\npackage MyApp::Website;\nuse Moose;\nhas url => (\ntraits => [qw/Labeled/],\nis     => 'rw',\nisa    => 'Str',\nlabel  => \"The site's URL\",\n);\nhas name => (\nis  => 'rw',\nisa => 'Str',\n);\nsub dump {\nmy $self = shift;\nmy $meta = $self->meta;\nmy $dump = '';\nfor my $attribute ( map { $meta->getattribute($) }\nsort $meta->getattributelist ) {\nif (   $attribute->does('MyApp::Meta::Attribute::Trait::Labeled')\n&& $attribute->haslabel ) {\n$dump .= $attribute->label;\n}\nelse {\n$dump .= $attribute->name;\n}\nmy $reader = $attribute->getreadmethod;\n$dump .= \": \" . $self->$reader . \"\\n\";\n}\nreturn $dump;\n}\npackage main;\nmy $app = MyApp::Website->new( url => \"http://google.com\", name => \"Google\" );",
    "sections": {
        "NAME": {
            "content": "Moose::Cookbook::Meta::LabeledAttributeTrait - Labels implemented via attribute traits\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 2.2207\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "package MyApp::Meta::Attribute::Trait::Labeled;\nuse Moose::Role;\nMoose::Util::metaattributealias('Labeled');\n\nhas label => (\nis        => 'rw',\nisa       => 'Str',\npredicate => 'haslabel',\n);\n\npackage MyApp::Website;\nuse Moose;\n\nhas url => (\ntraits => [qw/Labeled/],\nis     => 'rw',\nisa    => 'Str',\nlabel  => \"The site's URL\",\n);\n\nhas name => (\nis  => 'rw',\nisa => 'Str',\n);\n\nsub dump {\nmy $self = shift;\n\nmy $meta = $self->meta;\n\nmy $dump = '';\n\nfor my $attribute ( map { $meta->getattribute($) }\nsort $meta->getattributelist ) {\n\nif (   $attribute->does('MyApp::Meta::Attribute::Trait::Labeled')\n&& $attribute->haslabel ) {\n$dump .= $attribute->label;\n}\nelse {\n$dump .= $attribute->name;\n}\n\nmy $reader = $attribute->getreadmethod;\n$dump .= \": \" . $self->$reader . \"\\n\";\n}\n\nreturn $dump;\n}\n\npackage main;\n\nmy $app = MyApp::Website->new( url => \"http://google.com\", name => \"Google\" );\n",
            "subsections": []
        },
        "SUMMARY": {
            "content": "In this recipe, we begin to delve into the wonder of meta-programming. Some readers may scoff\nand claim that this is the arena of only the most twisted Moose developers. Absolutely not! Any\nsufficiently twisted developer can benefit greatly from going more meta.\n\nOur goal is to allow each attribute to have a human-readable \"label\" attached to it. Such labels\nwould be used when showing data to an end user. In this recipe we label the \"url\" attribute with\n\"The site's URL\" and create a simple method showing how to use that label.\n",
            "subsections": []
        },
        "META-ATTRIBUTE OBJECTS": {
            "content": "All the attributes of a Moose-based object are actually objects themselves. These objects have\nmethods and attributes. Let's look at a concrete example.\n\nhas 'x' => ( isa => 'Int', is => 'ro' );\nhas 'y' => ( isa => 'Int', is => 'rw' );\n\nInternally, the metaclass for \"Point\" has two Moose::Meta::Attribute objects. There are several\nmethods for getting meta-attributes out of a metaclass, one of which is \"getattributelist\".\nThis method is called on the metaclass object.\n\nThe \"getattributelist\" method returns a list of attribute names. You can then use\n\"getattribute\" to get the Moose::Meta::Attribute object itself.\n\nOnce you have this meta-attribute object, you can call methods on it like this:\n\nprint $point->meta->getattribute('x')->typeconstraint;\n=> Int\n\nTo add a label to our attributes there are two steps. First, we need a new attribute metaclass\ntrait that can store a label for an attribute. Second, we need to apply that trait to our\nattributes.\n",
            "subsections": []
        },
        "TRAITS": {
            "content": "Roles that apply to metaclasses have a special name: traits. Don't let the change in\nnomenclature fool you, traits are just roles.\n\n\"has\" in Moose allows you to pass a \"traits\" parameter for an attribute. This parameter takes a\nlist of trait names which are composed into an anonymous metaclass, and that anonymous metaclass\nis used for the attribute.\n\nYes, we still have lots of metaclasses in the background, but they're managed by Moose for you.\n\nTraits can do anything roles can do. They can add or refine attributes, wrap methods, provide\nmore methods, define an interface, etc. The only difference is that you're now changing the\nattribute metaclass instead of a user-level class.\n",
            "subsections": []
        },
        "DISSECTION": {
            "content": "We start by creating a package for our trait.\n\npackage MyApp::Meta::Attribute::Trait::Labeled;\nuse Moose::Role;\n\nhas label => (\nis        => 'rw',\nisa       => 'Str',\npredicate => 'haslabel',\n);\n\nYou can see that a trait is just a Moose::Role. In this case, our role contains a single\nattribute, \"label\". Any attribute which does this trait will now have a label.\n\nWe also register our trait with Moose:\n\nMoose::Util::metaattributealias('Labeled');\n\nThis allows Moose to find our trait by the short name \"Labeled\" when passed to the \"traits\"\nattribute option, rather than requiring the full package name to be specified.\n\nFinally, we pass our trait when defining an attribute:\n\nhas url => (\ntraits => [qw/Labeled/],\nis     => 'rw',\nisa    => 'Str',\nlabel  => \"The site's URL\",\n);\n\nThe \"traits\" parameter contains a list of trait names. Moose will build an anonymous attribute\nmetaclass from these traits and use it for this attribute.\n\nThe reason that we can pass the name \"Labeled\", instead of\n\"MyApp::Meta::Attribute::Trait::Labeled\", is because of the \"registerimplementation\" code we\ntouched on previously.\n\nWhen you pass a metaclass to \"has\", it will take the name you provide and prefix it with\n\"Moose::Meta::Attribute::Custom::Trait::\". Then it calls \"registerimplementation\" in the\npackage. In this case, that means Moose ends up calling \"registerimplementation\" from\n\"Moose::Meta::Attribute::Custom::Trait::Labeled\".\n\nIf this function exists, it should return the *real* trait's package name. This is exactly what\nour code does, returning \"MyApp::Meta::Attribute::Trait::Labeled\". This is a little convoluted,\nand if you don't like it, you can always use the fully-qualified name.\n\nWe can access this meta-attribute and its label like this:\n\n$website->meta->getattribute('url')->label()\n\nMyApp::Website->meta->getattribute('url')->label()\n\nWe also have a regular attribute, \"name\":\n\nhas name => (\nis  => 'rw',\nisa => 'Str',\n);\n\nFinally, we have a \"dump\" method, which creates a human-readable representation of a\n\"MyApp::Website\" object. It will use an attribute's label if it has one.\n\nsub dump {\nmy $self = shift;\n\nmy $meta = $self->meta;\n\nmy $dump = '';\n\nfor my $attribute ( map { $meta->getattribute($) }\nsort $meta->getattributelist ) {\n\nif (   $attribute->does('MyApp::Meta::Attribute::Trait::Labeled')\n&& $attribute->haslabel ) {\n$dump .= $attribute->label;\n}\n\nThis is a bit of defensive code. We cannot depend on every meta-attribute having a label. Even\nif we define one for every attribute in our class, a subclass may neglect to do so. Or a\nsuperclass could add an attribute without a label.\n\nWe also check that the attribute has a label using the predicate we defined. We could instead\nmake the label \"required\". If we have a label, we use it, otherwise we use the attribute name:\n\nelse {\n$dump .= $attribute->name;\n}\n\nmy $reader = $attribute->getreadmethod;\n$dump .= \": \" . $self->$reader . \"\\n\";\n}\n\nreturn $dump;\n}\n\nThe \"getreadmethod\" is part of the Moose::Meta::Attribute API. It returns the name of a method\nthat can read the attribute's value, *when called on the real object* (don't call this on the\nmeta-attribute).\n",
            "subsections": []
        },
        "CONCLUSION": {
            "content": "You might wonder why you'd bother with all this. You could just hardcode \"The Site's URL\" in the\n\"dump\" method. But we want to avoid repetition. If you need the label once, you may need it\nelsewhere, maybe in the \"asform\" method you write next.\n\nAssociating a label with an attribute just makes sense! The label is a piece of information\n*about* the attribute.\n\nIt's also important to realize that this was a trivial example. You can make much more powerful\nmetaclasses that *do* things, as opposed to just storing some more information. For example, you\ncould implement a metaclass that expires attributes after a certain amount of time:\n\nhas sitecache => (\ntraits        => ['TimedExpiry'],\nexpiresafter => { hours => 1 },\nrefreshwith  => sub { get( $[0]->url ) },\nisa           => 'Str',\nis            => 'ro',\n);\n\nThe sky's the limit!\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::LabeledAttributeTrait - Labels implemented via attribute traits",
    "flags": [],
    "examples": [],
    "see_also": []
}