The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

MooseX::Attribute::Prototype - Borrow and Extend Moose Attrtibutes

VERSION

0.05 - Released 2009-02-04

SYNOPSIS

    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'

DESCRIPTION

This module loads a metaclass role for attribute prototyping -- the practice of borrowing an attribute from a role and (possibly) extending or overriding the attributes definition. This works very similar to Moose's native attribute cloning, but allows for some other benefits such as changing the name of the attribute and the abstracting of attributes into roles.

Attributes are very often objects that have their own types and methods associated with them. MooseX::Attribute::Prototype takes a very functional view of roles. Attributes are the fundamental building blocks of class an application. This module allows better a better seperation of concerns by allowing better reuse and of attributes. Simple put all the functionality of a attribute in a role.

When your attribute specification declars a prototype, the role is loaded and the matching attribute is installed but overriden by current specifications. In many situations, all you will need to do is declare a prototype in . But sometimes you want to tweak beahaviors and/or defaults. MooseX::Attribute::Prototype allows the defaults to be overridden with those defined in the class. The resulting attribute specification is installed in the class.

How to use Attribute Prototypes

All variants of usage are in the SYNOPSIS above. But in case you want a more thorough explanations, Prototypes are just any good ole Moose attributes in any good ole Moose::Role. To use them simply declare a prototype in your attribute definition:

    prototype => 'MyRole/attribute' 

where role 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.

    prototype => 'MyRole' 

The attribute used as a prototype has the the same name as the role, except it has all lower-case letters. In this example, the attribute prototype is MyRole/myrole. This is just a shortcut to cover the very common occurrence where the attribute shares the name of the role.

WHY?

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.

Subclassing Benefit

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.

SEE ALSO

MooseX::Attribute::Prototype::Meta,

MooseX::Attribute::Prototype::Object,

MooseX::Attribute::Prototype::Collection,

Moose

MooseX::Role::Parameterized

MooseX::Types

AUTHOR

Christopher Brown, <ctbrown at cpan.org>

BUGS

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.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc MooseX::Attribute::Prototype

You can also look for information at:

ACKNOWLEDGEMENTS

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 & 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.