=pod =head1 NAME Moose::Cookbook::Meta::Recipe2 - A meta-attribute, attributes with labels =head1 SYNOPSIS package MyApp::Meta::Attribute::Labeled; use Moose; extends 'Moose::Meta::Attribute'; has label => ( is => 'rw', isa => 'Str', predicate => 'has_label', ); package Moose::Meta::Attribute::Custom::Labeled; sub register_implementation {'MyApp::Meta::Attribute::Labeled'} package MyApp::Website; use Moose; use MyApp::Meta::Attribute::Labeled; has url => ( metaclass => 'Labeled', is => 'rw', isa => 'Str', label => "The site's URL", ); has name => ( is => 'rw', isa => 'Str', ); sub dump { my $self = shift; # iterate over all the attributes in $self my %attributes = %{ $self->meta->get_attribute_map }; while ( my ( $name, $attribute ) = each %attributes ) { # print the label if available if ( $attribute->isa('MyApp::Meta::Attribute::Labeled') && $attribute->has_label ) { print $attribute->label; } # otherwise print the name else { print $name; } # print the attribute's value my $reader = $attribute->get_read_method; print ": " . $self->$reader . "\n"; } } package main; my $app = MyApp::Website->new( url => "http://google.com", name => "Google" ); $app->dump; =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. 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 hash reference that maps attribute names to their objects. In our case, C might return something that looks like the following: { x => $attr_object_for_x, y => $attr_object_for_y, } You can also get a single L with C. 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 that can store a label for an attribute. Second, we nede to create attributes that use that attribute metaclass. =head1 RECIPE REVIEW We start by creating a new attribute metaclass. package MyApp::Meta::Attribute::Labeled; use Moose; extends 'Moose::Meta::Attribute'; We can subclass a Moose metaclass in the same way that we subclass anything else. has label => ( is => 'rw', isa => 'Str', predicate => 'has_label', ); Again, this is standard Moose code. Then we need to register our metaclass with Moose: package Moose::Meta::Attribute::Custom::Labeled; sub register_implementation { 'MyApp::Meta::Attribute::Labeled' } This is a bit of magic that lets us use a short name, "Labeled", when referring to our new metaclas. That was the whole attribute metaclass. Now we start using it. package MyApp::Website; use Moose; use MyApp::Meta::Attribute::Labeled; We have to load the metaclass to use it, just like any Perl class. Finally, we use it for an attribute: has url => ( metaclass => 'Labeled', is => 'rw', isa => 'Str', label => "The site's URL", ); This looks like a normal attribute declaraion, except for two things, the C and C