- How to use Attribute Prototypes
- SEE ALSO
- COPYRIGHT & LICENSE
MooseX::Attribute::Prototype - Borrow and Extend Moose Attrtibutes
0.10 - Released 2009-07-18
package MyClass; use Moose; use MooseX::Attribute::Prototype; has 'my_attr' => ( is => 'rw' , isa => 'Str' , prototype => 'MyRole/my_attr' , ); has 'my_attr_2' => prototype => 'MyRole2/my_attr_2'; has 'my_attr_3' => prototype => 'MyRole3'; # Same as 'MyRole3/myrole3'
This module implements attribute prototyping -- the practice of borrowing an attribute from a role and optionally overriding/extending the attribute definition. This is This works very similar to Moose's native attribute cloning, but allows for additional benefits such as changing the name of the attribute and the abstracting of attributes into roles.
Attributes are very often designed as objects that have their own types and methods associated with them. MooseX::Attribute::Prototype takes a very pragmatic view of attributes. They are the fundamental building blocks of a class. This module promotes a more natural reuse of attributes.
When your attribute includes a
prototype specification, the attribute is copied from the role and attribute. In many situations, all you will want is declare a
prototype. All current specifications override those provided by prototype.
How to use Attribute Prototypes
All variants of usage are in the SYNOPSIS above. This is a more thorough explanation.
Prototypes are just any good ole Moose attributes in good ole Moose::Role. To use them simply declare a
prototype in your attribute definition:
prototype => 'MyRole/attribute'
MyRole is the name of the role and
attribute is the name of the attribute. As of version 0.05, you may use the abbreviated specification and omit the name of the
attribute. The attribute used as the prototype has the the same name as the role, except it has all lower-case letters.
prototype => 'MyRole'
In this example, the prototype is
MyRole/myrole serves as the prototype. This is just a shortcut to cover the very common occurrence where the attribute shares the name of the role.
MooseX::Role::Parameterized and MooseX::Types abstract the roles and types, respectively. But surprisinly, there is no similar functionality for attributes. Moose leans towards viewing attributes as containers for data. However, attributes can store full-fledged objects. And these objects often have specialized types and subtypes, methods, and behaviors (such as getting their values using MooseX::Getopt). In fact, attribute specifications, can often become the majority of code for a given application. Why not seperate these chunks into horizontally-reusable roles?
MooseX::Attribute::Prototype takes a functional view of attributes -- slots that can contain anything -- and provides an easy interface for making these slots reusable.
Why Not Moose's Attribute Clone Mechanism?
Moose's attribute cloning does not allow you to change the name of the derived attribute. You can take the defaults of an attribute from a role and change its default, but good luck in changing the name of the attribute.
Moose makes subclassing easy through the c<extends> sugar. More often than not, however, Moose applications are an amalgam of objects including other Moose classes and other CPAN modules. In these cases, one often places the objects in the the attributes. MooseX::Attributes::Prototypes allows for the Moosifying of these CPAN classes in a reusable way.
<ctbrown at cpan.org>
Please report any bugs or feature requests to
bug-moosex-attribute-prototype at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-Attribute-Prototype. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
You can find documentation for this module with the perldoc command.
You can also look for information at:
RT: CPAN's request tracker
AnnoCPAN: Annotated CPAN documentation
Though they would probably cringe to hear it, this effort would not have been possible without:
COPYRIGHT & LICENSE
Copyright 2009 Christopher Brown and Open Data Group http://opendatagroup.com.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.