MooseX::Attribute - create extendable and reusable attributes
Version 0.02
This is still an experimental module and should not be used in production systems. It is very, very likely to change. The parts that may change are:
- package name(s): - attribute(sugar) names: seems like a bad name ... has_? or just replace has
Create a prototype attribute
package MyRole; use Moose::Role; use MooseX::Attribute; attribute file => ( is => 'ro' , isa => 'Str' , required=> 0 ); no MooseX::Attribute;
Then in your class, borrow that attribute
package MyApp; use Moose; use MooseX::Attribute::Consumer; with 'MyRole'; attribute infile => ( from_role => 'MyRole' , prototype => 'file' required => 1 ); ... package main; MyApp->new( infile => '/tmp/data' );
MooseX::Attribute provides Moose sugar to abstract, import and extend Moose attributes. Already, types have been made abstracted by MooseX::Types. Roles have been abstracted by MooseX::Role::Parameterized. This is just an extension of those abstractions applied to attributes.
Adding attributes to class should be easy as an import statement. Most attributes are very recurring, e.g. a date attribute, a file attribute or a URI attribute. Nonetheless, these are often re-coded for each application. This module presents a mechanism to obviate that recoding.
In most cases and with sensible defaults specifications. Nothing need be changed to get a fully functional attribute. When these defaults are inadequate, overriding and extending is easy and follows existing attribute. The developer need not know the complete specification for the behaviors of the attributes, he only need worry about the deltas!
There are several goals:
1. Make attribute specifications (more) reusable 2. Make it easy/easier to 'Moosify' existing CPAN modules/objects as Moose attributes. 3. Use Moose and follow the Moose design closely.
attribute is Moose sugar word that allows you to import and extend Moose attributes that are defined in Moose roles. For most purposes, you can use attribute in place. See #LIMITATIONS for when this breaks.
attribute
has attr_1 => ( ... ); attribute attr_2 => ( ... );
attribute can be used in all(?) four attribute situations:
1. The attribute is specified directly in the consuming class just as 'has' is used. No other attribute references this attribute. 2. The attribute is defined in in a Moose role consumed by the class/ No other attributes references this attribute as a prototype. 3. The attribute specified another attribute as its prototype. The prototype lives in the same package. 4. The attribute specifies another attribute as its prototype. The prototype lives in another package.
attribute delays the building of attributes. Instead, it keeps a list of attribute_prototypes and attribute_installs. A method modifier is then used to define these attribute prior to the class being initialized. This is similar to how roles are dealt with. In fact, although not done in this module, it would allow you to create methods that could apply options to all attributes in a single method call. At present, I do not believe this is (easily) possible.
attribute_prototypes
attribute_installs
At present, simultaneously consuming an attribute from a role and using it as a prototype cannot work. The straight consumption will be overlooked. If you use an attribute as a prototype, all uses of that attribute must follow the prototype specification.
MooseX::Attribute::Consumer Module for consuming prototype roles.
Christopher Brown, <ctbrown at cpan.org>
<ctbrown at cpan.org>
Please report any bugs or feature requests to bug-moosex-attribute at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-Attribute. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-moosex-attribute at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc MooseX::Attribute
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-Attribute
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/MooseX-Attribute
CPAN Ratings
http://cpanratings.perl.org/d/MooseX-Attribute
Search CPAN
http://search.cpan.org/dist/MooseX-Attribute
Though they would probably cringe to hear it, this effort would not have been possible without:
Shawn Moore
David Rolsky
Thomas Doran
Stevan Little
Copyright 2008 Christopher Brown and Open Data Group http://opendatagroup.com, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install MooseX::Attribute, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::Attribute
CPAN shell
perl -MCPAN -e shell install MooseX::Attribute
For more information on module installation, please visit the detailed CPAN module installation guide.