.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Moose::Cookbook::Meta::Labeled_AttributeTrait 3pm" .TH Moose::Cookbook::Meta::Labeled_AttributeTrait 3pm 2024-03-07 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Moose::Cookbook::Meta::Labeled_AttributeTrait \- Labels implemented via attribute traits .SH VERSION .IX Header "VERSION" version 2.2207 .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 3 \& package MyApp::Meta::Attribute::Trait::Labeled; \& use Moose::Role; \& Moose::Util::meta_attribute_alias(\*(AqLabeled\*(Aq); \& \& has label => ( \& is => \*(Aqrw\*(Aq, \& isa => \*(AqStr\*(Aq, \& predicate => \*(Aqhas_label\*(Aq, \& ); \& \& package MyApp::Website; \& use Moose; \& \& has url => ( \& traits => [qw/Labeled/], \& is => \*(Aqrw\*(Aq, \& isa => \*(AqStr\*(Aq, \& label => "The site\*(Aqs URL", \& ); \& \& has name => ( \& is => \*(Aqrw\*(Aq, \& isa => \*(AqStr\*(Aq, \& ); \& \& sub dump { \& my $self = shift; \& \& my $meta = $self\->meta; \& \& my $dump = \*(Aq\*(Aq; \& \& for my $attribute ( map { $meta\->get_attribute($_) } \& sort $meta\->get_attribute_list ) { \& \& if ( $attribute\->does(\*(AqMyApp::Meta::Attribute::Trait::Labeled\*(Aq) \& && $attribute\->has_label ) { \& $dump .= $attribute\->label; \& } \& else { \& $dump .= $attribute\->name; \& } \& \& my $reader = $attribute\->get_read_method; \& $dump .= ": " . $self\->$reader . "\en"; \& } \& \& return $dump; \& } \& \& package main; \& \& my $app = MyApp::Website\->new( url => "http://google.com", name => "Google" ); .Ve .SH SUMMARY .IX Header "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. .PP 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 \f(CW\*(C`url\*(C'\fR attribute with "The site's URL" and create a simple method showing how to use that label. .SH "META-ATTRIBUTE OBJECTS" .IX Header "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. .PP .Vb 2 \& has \*(Aqx\*(Aq => ( isa => \*(AqInt\*(Aq, is => \*(Aqro\*(Aq ); \& has \*(Aqy\*(Aq => ( isa => \*(AqInt\*(Aq, is => \*(Aqrw\*(Aq ); .Ve .PP Internally, the metaclass for \f(CW\*(C`Point\*(C'\fR has two Moose::Meta::Attribute objects. There are several methods for getting meta-attributes out of a metaclass, one of which is \f(CW\*(C`get_attribute_list\*(C'\fR. This method is called on the metaclass object. .PP The \f(CW\*(C`get_attribute_list\*(C'\fR method returns a list of attribute names. You can then use \f(CW\*(C`get_attribute\*(C'\fR to get the Moose::Meta::Attribute object itself. .PP Once you have this meta-attribute object, you can call methods on it like this: .PP .Vb 2 \& print $point\->meta\->get_attribute(\*(Aqx\*(Aq)\->type_constraint; \& => Int .Ve .PP 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. .SH TRAITS .IX Header "TRAITS" Roles that apply to metaclasses have a special name: traits. Don't let the change in nomenclature fool you, \fBtraits are just roles\fR. .PP "has" in Moose allows you to pass a \f(CW\*(C`traits\*(C'\fR 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. .PP Yes, we still have lots of metaclasses in the background, but they're managed by Moose for you. .PP 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. .SH DISSECTION .IX Header "DISSECTION" We start by creating a package for our trait. .PP .Vb 2 \& package MyApp::Meta::Attribute::Trait::Labeled; \& use Moose::Role; \& \& has label => ( \& is => \*(Aqrw\*(Aq, \& isa => \*(AqStr\*(Aq, \& predicate => \*(Aqhas_label\*(Aq, \& ); .Ve .PP You can see that a trait is just a Moose::Role. In this case, our role contains a single attribute, \f(CW\*(C`label\*(C'\fR. Any attribute which does this trait will now have a label. .PP We also register our trait with Moose: .PP .Vb 1 \& Moose::Util::meta_attribute_alias(\*(AqLabeled\*(Aq); .Ve .PP This allows Moose to find our trait by the short name \f(CW\*(C`Labeled\*(C'\fR when passed to the \f(CW\*(C`traits\*(C'\fR attribute option, rather than requiring the full package name to be specified. .PP Finally, we pass our trait when defining an attribute: .PP .Vb 6 \& has url => ( \& traits => [qw/Labeled/], \& is => \*(Aqrw\*(Aq, \& isa => \*(AqStr\*(Aq, \& label => "The site\*(Aqs URL", \& ); .Ve .PP The \f(CW\*(C`traits\*(C'\fR parameter contains a list of trait names. Moose will build an anonymous attribute metaclass from these traits and use it for this attribute. .PP The reason that we can pass the name \f(CW\*(C`Labeled\*(C'\fR, instead of \&\f(CW\*(C`MyApp::Meta::Attribute::Trait::Labeled\*(C'\fR, is because of the \&\f(CW\*(C`register_implementation\*(C'\fR code we touched on previously. .PP When you pass a metaclass to \f(CW\*(C`has\*(C'\fR, it will take the name you provide and prefix it with \f(CW\*(C`Moose::Meta::Attribute::Custom::Trait::\*(C'\fR. Then it calls \&\f(CW\*(C`register_implementation\*(C'\fR in the package. In this case, that means Moose ends up calling \f(CW\*(C`register_implementation\*(C'\fR from \&\f(CW\*(C`Moose::Meta::Attribute::Custom::Trait::Labeled\*(C'\fR. .PP If this function exists, it should return the \fIreal\fR trait's package name. This is exactly what our code does, returning \&\f(CW\*(C`MyApp::Meta::Attribute::Trait::Labeled\*(C'\fR. This is a little convoluted, and if you don't like it, you can always use the fully-qualified name. .PP We can access this meta-attribute and its label like this: .PP .Vb 1 \& $website\->meta\->get_attribute(\*(Aqurl\*(Aq)\->label() \& \& MyApp::Website\->meta\->get_attribute(\*(Aqurl\*(Aq)\->label() .Ve .PP We also have a regular attribute, \f(CW\*(C`name\*(C'\fR: .PP .Vb 4 \& has name => ( \& is => \*(Aqrw\*(Aq, \& isa => \*(AqStr\*(Aq, \& ); .Ve .PP Finally, we have a \f(CW\*(C`dump\*(C'\fR method, which creates a human-readable representation of a \f(CW\*(C`MyApp::Website\*(C'\fR object. It will use an attribute's label if it has one. .PP .Vb 2 \& sub dump { \& my $self = shift; \& \& my $meta = $self\->meta; \& \& my $dump = \*(Aq\*(Aq; \& \& for my $attribute ( map { $meta\->get_attribute($_) } \& sort $meta\->get_attribute_list ) { \& \& if ( $attribute\->does(\*(AqMyApp::Meta::Attribute::Trait::Labeled\*(Aq) \& && $attribute\->has_label ) { \& $dump .= $attribute\->label; \& } .Ve .PP This is a bit of defensive code. We cannot depend on every meta-attribute having a label. Even if we define one for every attribute in our class, a subclass may neglect to do so. Or a superclass could add an attribute without a label. .PP We also check that the attribute has a label using the predicate we defined. We could instead make the label \f(CW\*(C`required\*(C'\fR. If we have a label, we use it, otherwise we use the attribute name: .PP .Vb 3 \& else { \& $dump .= $attribute\->name; \& } \& \& my $reader = $attribute\->get_read_method; \& $dump .= ": " . $self\->$reader . "\en"; \& } \& \& return $dump; \& } .Ve .PP The \f(CW\*(C`get_read_method\*(C'\fR is part of the Moose::Meta::Attribute API. It returns the name of a method that can read the attribute's value, \fIwhen called on the real object\fR (don't call this on the meta-attribute). .SH CONCLUSION .IX Header "CONCLUSION" You might wonder why you'd bother with all this. You could just hardcode "The Site's URL" in the \f(CW\*(C`dump\*(C'\fR method. But we want to avoid repetition. If you need the label once, you may need it elsewhere, maybe in the \f(CW\*(C`as_form\*(C'\fR method you write next. .PP Associating a label with an attribute just makes sense! The label is a piece of information \fIabout\fR the attribute. .PP It's also important to realize that this was a trivial example. You can make much more powerful metaclasses that \fIdo\fR things, as opposed to just storing some more information. For example, you could implement a metaclass that expires attributes after a certain amount of time: .PP .Vb 7 \& has site_cache => ( \& traits => [\*(AqTimedExpiry\*(Aq], \& expires_after => { hours => 1 }, \& refresh_with => sub { get( $_[0]\->url ) }, \& isa => \*(AqStr\*(Aq, \& is => \*(Aqro\*(Aq, \& ); .Ve .PP The sky's the limit! .SH AUTHORS .IX Header "AUTHORS" .IP \(bu 4 Stevan Little .IP \(bu 4 Dave Rolsky .IP \(bu 4 Jesse Luehrs .IP \(bu 4 Shawn M Moore .IP \(bu 4 יובל קוג'מן (Yuval Kogman) .IP \(bu 4 Karen Etheridge .IP \(bu 4 Florian Ragwitz .IP \(bu 4 Hans Dieter Pearcey .IP \(bu 4 Chris Prather .IP \(bu 4 Matt S Trout .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2006 by Infinity Interactive, Inc. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.