1 package Moose::Cookbook::Meta::Labeled_AttributeMetaclass;
3 # ABSTRACT: A meta-attribute, attributes with labels
12 package MyApp::Meta::Attribute::Labeled;
14 extends 'Moose::Meta::Attribute';
19 predicate => 'has_label',
22 package Moose::Meta::Attribute::Custom::Labeled;
23 sub register_implementation {'MyApp::Meta::Attribute::Labeled'}
25 package MyApp::Website;
29 metaclass => 'Labeled',
32 label => "The site's URL",
43 my $meta = $self->meta;
47 for my $attribute ( map { $meta->get_attribute($_) }
48 sort $meta->get_attribute_list ) {
50 if ( $attribute->isa('MyApp::Meta::Attribute::Labeled')
51 && $attribute->has_label ) {
52 $dump .= $attribute->label;
55 $dump .= $attribute->name;
58 my $reader = $attribute->get_read_method;
59 $dump .= ": " . $self->$reader . "\n";
67 my $app = MyApp::Website->new( url => "http://google.com", name => "Google" );
71 B<WARNING: Subclassing metaclasses (as opposed to providing metaclass traits)
72 is strongly discouraged. This recipe is provided solely for reference when
73 encountering older code that does this.>
75 In this recipe, we begin to delve into the wonder of meta-programming.
76 Some readers may scoff and claim that this is the arena of only the
77 most twisted Moose developers. Absolutely not! Any sufficiently
78 twisted developer can benefit greatly from going more meta.
80 Our goal is to allow each attribute to have a human-readable "label"
81 attached to it. Such labels would be used when showing data to an end
82 user. In this recipe we label the C<url> attribute with "The site's
83 URL" and create a simple method showing how to use that label.
85 The proper, modern way to extend attributes (using a role instead of a
86 subclass) is described in L<Moose::Cookbook::Meta::Recipe3>, but that recipe
87 assumes you've read and at least tried to understand this one.
89 =head1 META-ATTRIBUTE OBJECTS
91 All the attributes of a Moose-based object are actually objects
92 themselves. These objects have methods and attributes. Let's look at
95 has 'x' => ( isa => 'Int', is => 'ro' );
96 has 'y' => ( isa => 'Int', is => 'rw' );
98 Internally, the metaclass for C<Point> has two
99 L<Moose::Meta::Attribute>. There are several methods for getting
100 meta-attributes out of a metaclass, one of which is
101 C<get_attribute_list>. This method is called on the metaclass object.
103 The C<get_attribute_list> method returns a list of attribute names. You can
104 then use C<get_attribute> to get the L<Moose::Meta::Attribute> object itself.
106 Once you have this meta-attribute object, you can call methods on it like this:
108 print $point->meta->get_attribute('x')->type_constraint;
111 To add a label to our attributes there are two steps. First, we need a
112 new attribute metaclass that can store a label for an
113 attribute. Second, we need to create attributes that use that
118 We start by creating a new attribute metaclass.
120 package MyApp::Meta::Attribute::Labeled;
122 extends 'Moose::Meta::Attribute';
124 We can subclass a Moose metaclass in the same way that we subclass
130 predicate => 'has_label',
133 Again, this is standard Moose code.
135 Then we need to register our metaclass with Moose:
137 package Moose::Meta::Attribute::Custom::Labeled;
138 sub register_implementation { 'MyApp::Meta::Attribute::Labeled' }
140 This is a bit of magic that lets us use a short name, "Labeled", when
141 referring to our new metaclass.
143 That was the whole attribute metaclass.
145 Now we start using it.
147 package MyApp::Website;
149 use MyApp::Meta::Attribute::Labeled;
151 We have to load the metaclass to use it, just like any Perl class.
153 Finally, we use it for an attribute:
156 metaclass => 'Labeled',
159 label => "The site's URL",
162 This looks like a normal attribute declaration, except for two things,
163 the C<metaclass> and C<label> parameters. The C<metaclass> parameter
164 tells Moose we want to use a custom metaclass for this (one)
165 attribute. The C<label> parameter will be stored in the meta-attribute
168 The reason that we can pass the name C<Labeled>, instead of
169 C<MyApp::Meta::Attribute::Labeled>, is because of the
170 C<register_implementation> code we touched on previously.
172 When you pass a metaclass to C<has>, it will take the name you provide
173 and prefix it with C<Moose::Meta::Attribute::Custom::>. Then it calls
174 C<register_implementation> in the package. In this case, that means
175 Moose ends up calling
176 C<Moose::Meta::Attribute::Custom::Labeled::register_implementation>.
178 If this function exists, it should return the I<real> metaclass
179 package name. This is exactly what our code does, returning
180 C<MyApp::Meta::Attribute::Labeled>. This is a little convoluted, and
181 if you don't like it, you can always use the fully-qualified name.
183 We can access this meta-attribute and its label like this:
185 $website->meta->get_attribute('url')->label()
187 MyApp::Website->meta->get_attribute('url')->label()
189 We also have a regular attribute, C<name>:
196 This is a regular Moose attribute, because we have not specified a new
199 Finally, we have a C<dump> method, which creates a human-readable
200 representation of a C<MyApp::Website> object. It will use an
201 attribute's label if it has one.
206 my $meta = $self->meta;
210 for my $attribute ( map { $meta->get_attribute($_) }
211 sort $meta->get_attribute_list ) {
213 if ( $attribute->isa('MyApp::Meta::Attribute::Labeled')
214 && $attribute->has_label ) {
215 $dump .= $attribute->label;
218 This is a bit of defensive code. We cannot depend on every
219 meta-attribute having a label. Even if we define one for every
220 attribute in our class, a subclass may neglect to do so. Or a
221 superclass could add an attribute without a label.
223 We also check that the attribute has a label using the predicate we
224 defined. We could instead make the label C<required>. If we have a
225 label, we use it, otherwise we use the attribute name:
228 $dump .= $attribute->name;
231 my $reader = $attribute->get_read_method;
232 $dump .= ": " . $self->$reader . "\n";
238 The C<get_read_method> is part of the L<Moose::Meta::Attribute>
239 API. It returns the name of a method that can read the attribute's
240 value, I<when called on the real object> (don't call this on the
245 You might wonder why you'd bother with all this. You could just
246 hardcode "The Site's URL" in the C<dump> method. But we want to avoid
247 repetition. If you need the label once, you may need it elsewhere,
248 maybe in the C<as_form> method you write next.
250 Associating a label with an attribute just makes sense! The label is a
251 piece of information I<about> the attribute.
253 It's also important to realize that this was a trivial example. You
254 can make much more powerful metaclasses that I<do> things, as opposed
255 to just storing some more information. For example, you could
256 implement a metaclass that expires attributes after a certain amount
260 metaclass => 'TimedExpiry',
261 expires_after => { hours => 1 },
262 refresh_with => sub { get( $_[0]->url ) },
271 my $app = MyApp::Website->new( url => "http://google.com", name => "Google" );
273 $app->dump, q{name: Google
274 The site's URL: http://google.com
275 }, '... got the expected dump value'