# PODNAME: Moose::Cookbook::Meta::Labeled_AttributeTrait # ABSTRACT: Labels implemented via attribute traits __END__ =pod =encoding UTF-8 =head1 NAME Moose::Cookbook::Meta::Labeled_AttributeTrait - Labels implemented via attribute traits =head1 VERSION version 2.2207 =head1 SYNOPSIS package MyApp::Meta::Attribute::Trait::Labeled; use Moose::Role; Moose::Util::meta_attribute_alias('Labeled'); has label => ( is => 'rw', isa => 'Str', predicate => 'has_label', ); package MyApp::Website; use Moose; has url => ( traits => [qw/Labeled/], is => 'rw', isa => 'Str', label => "The site's URL", ); has name => ( is => 'rw', isa => 'Str', ); sub dump { my $self = shift; my $meta = $self->meta; my $dump = ''; for my $attribute ( map { $meta->get_attribute($_) } sort $meta->get_attribute_list ) { if ( $attribute->does('MyApp::Meta::Attribute::Trait::Labeled') && $attribute->has_label ) { $dump .= $attribute->label; } else { $dump .= $attribute->name; } my $reader = $attribute->get_read_method; $dump .= ": " . $self->$reader . "\n"; } return $dump; } package main; my $app = MyApp::Website->new( url => "http://google.com", name => "Google" ); =head1 SUMMARY In this recipe, we begin to delve into the wonder of meta-programming. Some readers may scoff and claim that this is the arena of only the most twisted Moose developers. Absolutely not! Any sufficiently twisted developer can benefit greatly from going more meta. Our goal is to allow each attribute to have a human-readable "label" attached to it. Such labels would be used when showing data to an end user. In this recipe we label the C attribute with "The site's URL" and create a simple method showing how to use that label. =head1 META-ATTRIBUTE OBJECTS All the attributes of a Moose-based object are actually objects themselves. These objects have methods and attributes. Let's look at a concrete example. has 'x' => ( isa => 'Int', is => 'ro' ); has 'y' => ( isa => 'Int', is => 'rw' ); Internally, the metaclass for C has two L objects. There are several methods for getting meta-attributes out of a metaclass, one of which is C. This method is called on the metaclass object. The C method returns a list of attribute names. You can then use C to get the L object itself. Once you have this meta-attribute object, you can call methods on it like this: print $point->meta->get_attribute('x')->type_constraint; => Int To add a label to our attributes there are two steps. First, we need a new attribute metaclass trait that can store a label for an attribute. Second, we need to apply that trait to our attributes. =head1 TRAITS Roles that apply to metaclasses have a special name: traits. Don't let the change in nomenclature fool you, B. L allows you to pass a C parameter for an attribute. This parameter takes a list of trait names which are composed into an anonymous metaclass, and that anonymous metaclass is used for the attribute. Yes, we still have lots of metaclasses in the background, but they're managed by Moose for you. Traits can do anything roles can do. They can add or refine attributes, wrap methods, provide more methods, define an interface, etc. The only difference is that you're now changing the attribute metaclass instead of a user-level class. =head1 DISSECTION We start by creating a package for our trait. package MyApp::Meta::Attribute::Trait::Labeled; use Moose::Role; has label => ( is => 'rw', isa => 'Str', predicate => 'has_label', ); You can see that a trait is just a L. In this case, our role contains a single attribute, C