1 package Moose::Cookbook::Meta::Recipe3;
3 # ABSTRACT: Labels implemented via attribute traits
12 package MyApp::Meta::Attribute::Trait::Labeled;
18 predicate => 'has_label',
21 package Moose::Meta::Attribute::Custom::Trait::Labeled;
22 sub register_implementation {'MyApp::Meta::Attribute::Trait::Labeled'}
24 package MyApp::Website;
28 traits => [qw/Labeled/],
31 label => "The site's URL",
42 my $meta = $self->meta;
46 for my $attribute ( map { $meta->get_attribute($_) }
47 sort $meta->get_attribute_list ) {
49 if ( $attribute->does('MyApp::Meta::Attribute::Trait::Labeled')
50 && $attribute->has_label ) {
51 $dump .= $attribute->label;
54 $dump .= $attribute->name;
57 my $reader = $attribute->get_read_method;
58 $dump .= ": " . $self->$reader . "\n";
66 my $app = MyApp::Website->new( url => "http://google.com", name => "Google" );
70 In this recipe, we begin to delve into the wonder of meta-programming.
71 Some readers may scoff and claim that this is the arena of only the
72 most twisted Moose developers. Absolutely not! Any sufficiently
73 twisted developer can benefit greatly from going more meta.
75 Our goal is to allow each attribute to have a human-readable "label"
76 attached to it. Such labels would be used when showing data to an end
77 user. In this recipe we label the C<url> attribute with "The site's
78 URL" and create a simple method showing how to use that label.
80 =head1 META-ATTRIBUTE OBJECTS
82 All the attributes of a Moose-based object are actually objects themselves.
83 These objects have methods and attributes. Let's look at a concrete example.
85 has 'x' => ( isa => 'Int', is => 'ro' );
86 has 'y' => ( isa => 'Int', is => 'rw' );
88 Internally, the metaclass for C<Point> has two L<Moose::Meta::Attribute>
89 objects. There are several methods for getting meta-attributes out of a
90 metaclass, one of which is C<get_attribute_list>. This method is called on the
93 The C<get_attribute_list> method returns a list of attribute names. You can
94 then use C<get_attribute> to get the L<Moose::Meta::Attribute> object itself.
96 Once you have this meta-attribute object, you can call methods on it like
99 print $point->meta->get_attribute('x')->type_constraint;
102 To add a label to our attributes there are two steps. First, we need a new
103 attribute metaclass trait that can store a label for an attribute. Second, we
104 need to apply that trait to our attributes.
108 Roles that apply to metaclasses have a special name: traits. Don't let
109 the change in nomenclature fool you, B<traits are just roles>.
111 L<Moose/has> allows you to pass a C<traits> parameter for an
112 attribute. This parameter takes a list of trait names which are
113 composed into an anonymous metaclass, and that anonymous metaclass is
114 used for the attribute.
116 Yes, we still have lots of metaclasses in the background, but they're
117 managed by Moose for you.
119 Traits can do anything roles can do. They can add or refine
120 attributes, wrap methods, provide more methods, define an interface,
121 etc. The only difference is that you're now changing the attribute
122 metaclass instead of a user-level class.
126 We start by creating a package for our trait.
128 package MyApp::Meta::Attribute::Trait::Labeled;
134 predicate => 'has_label',
137 You can see that a trait is just a L<Moose::Role>. In this case, our role
138 contains a single attribute, C<label>. Any attribute which does this trait
139 will now have a label.
141 Next we register our trait with Moose:
143 package Moose::Meta::Attribute::Custom::Trait::Labeled;
144 sub register_implementation { 'MyApp::Meta::Attribute::Trait::Labeled' }
146 Moose looks for the C<register_implementation> method in
147 C<Moose::Meta::Attribute::Custom::Trait::$TRAIT_NAME> to find the full
150 Finally, we pass our trait when defining an attribute:
153 traits => [qw/Labeled/],
156 label => "The site's URL",
159 The C<traits> parameter contains a list of trait names. Moose will build an
160 anonymous attribute metaclass from these traits and use it for this
163 The reason that we can pass the name C<Labeled>, instead of
164 C<MyApp::Meta::Attribute::Trait::Labeled>, is because of the
165 C<register_implementation> code we touched on previously.
167 When you pass a metaclass to C<has>, it will take the name you provide and
168 prefix it with C<Moose::Meta::Attribute::Custom::Trait::>. Then it calls
169 C<register_implementation> in the package. In this case, that means Moose ends
171 C<Moose::Meta::Attribute::Custom::Trait::Labeled::register_implementation>.
173 If this function exists, it should return the I<real> trait's package
174 name. This is exactly what our code does, returning
175 C<MyApp::Meta::Attribute::Trait::Labeled>. This is a little convoluted, and if
176 you don't like it, you can always use the fully-qualified name.
178 We can access this meta-attribute and its label like this:
180 $website->meta->get_attribute('url')->label()
182 MyApp::Website->meta->get_attribute('url')->label()
184 We also have a regular attribute, C<name>:
191 Finally, we have a C<dump> method, which creates a human-readable
192 representation of a C<MyApp::Website> object. It will use an attribute's label
198 my $meta = $self->meta;
202 for my $attribute ( map { $meta->get_attribute($_) }
203 sort $meta->get_attribute_list ) {
205 if ( $attribute->does('MyApp::Meta::Attribute::Trait::Labeled')
206 && $attribute->has_label ) {
207 $dump .= $attribute->label;
210 This is a bit of defensive code. We cannot depend on every meta-attribute
211 having a label. Even if we define one for every attribute in our class, a
212 subclass may neglect to do so. Or a superclass could add an attribute without
215 We also check that the attribute has a label using the predicate we
216 defined. We could instead make the label C<required>. If we have a label, we
217 use it, otherwise we use the attribute name:
220 $dump .= $attribute->name;
223 my $reader = $attribute->get_read_method;
224 $dump .= ": " . $self->$reader . "\n";
230 The C<get_read_method> is part of the L<Moose::Meta::Attribute> API. It
231 returns the name of a method that can read the attribute's value, I<when
232 called on the real object> (don't call this on the meta-attribute).
236 You might wonder why you'd bother with all this. You could just hardcode "The
237 Site's URL" in the C<dump> method. But we want to avoid repetition. If you
238 need the label once, you may need it elsewhere, maybe in the C<as_form> method
241 Associating a label with an attribute just makes sense! The label is a piece
242 of information I<about> the attribute.
244 It's also important to realize that this was a trivial example. You can make
245 much more powerful metaclasses that I<do> things, as opposed to just storing
246 some more information. For example, you could implement a metaclass that
247 expires attributes after a certain amount of time:
250 traits => ['TimedExpiry'],
251 expires_after => { hours => 1 },
252 refresh_with => sub { get( $_[0]->url ) },
262 = MyApp::Website->new( url => 'http://google.com', name => 'Google' );
264 $app->dump, q{name: Google
265 The site's URL: http://google.com
266 }, '... got the expected dump value'