Moose::Meta::Attribute - The Moose attribute metaclass



NAME

Moose::Meta::Attribute - The Moose attribute metaclass


DESCRIPTION

This is a subclass of the Class::MOP::Attribute manpage with Moose specific extensions.

For the most part, the only time you will ever encounter an instance of this class is if you are doing some serious deep introspection. To really understand this class, you need to refer to the the Class::MOP::Attribute manpage documentation.


METHODS

Overridden methods

These methods override methods in the Class::MOP::Attribute manpage and add Moose specific features. You can safely assume though that they will behave just as the Class::MOP::Attribute manpage does.

new
clone
does
initialize_instance_slot
install_accessors
accessor_metaclass
get_value
set_value
  eval { $point->meta->get_attribute('x')->set_value($point, 'fourty-two') };
  if($@) {
    print "Oops: $@\n";
  }

Attribute (x) does not pass the type constraint (Int) with 'fourty-two'

Before setting the value, a check is made on the type constraint of the attribute, if it has one, to see if the value passes it. If the value fails to pass, the set operation dies with a confess in the Carp manpage.

Any coercion to convert values is done before checking the type constraint.

To check a value against a type constraint before setting it, fetch the attribute instance using find_attribute_by_name in the Class::MOP::Class manpage, fetch the type_constraint from the attribute using type_constraint in the Moose::Meta::Attribute manpage and call check in the Moose::Meta::TypeConstraint manpage. See the Moose::Cookbook::RecipeX manpage for an example.

Additional Moose features

Moose attributes support type-constraint checking, weak reference creation and type coercion.

interpolate_class_and_new
interpolate_class
When called as a class method causes interpretation of the metaclass and traits options.

clone_and_inherit_options
This is to support the has '+foo' feature, it clones an attribute from a superclass and allows a very specific set of changes to be made to the attribute.

has_type_constraint
Returns true if this meta-attribute has a type constraint.

type_constraint
A read-only accessor for this meta-attribute's type constraint. For more information on what you can do with this, see the documentation for the Moose::Meta::TypeConstraint manpage.

has_handles
Returns true if this meta-attribute performs delegation.

handles
This returns the value which was passed into the handles option.

is_weak_ref
Returns true if this meta-attribute produces a weak reference.

is_required
Returns true if this meta-attribute is required to have a value.

is_lazy
Returns true if this meta-attribute should be initialized lazily.

NOTE: lazy attributes, must have a default or builder field set.

is_lazy_build
Returns true if this meta-attribute should be initialized lazily through the builder generated by lazy_build. Using lazy_build => 1 will make your attribute required and lazy. In addition it will set the builder, clearer and predicate options for you using the following convention.
   #If your attribute name starts with an underscore:
   has '_foo' => (lazy_build => 1);
   #is the same as
   has '_foo' => (lazy => 1, required => 1, predicate => '_has_foo', clearer => '_clear_foo', builder => '_build__foo);
   # or
   has '_foo' => (lazy => 1, required => 1, predicate => '_has_foo', clearer => '_clear_foo', default => sub{shift->_build__foo});
   #If your attribute name does not start with an underscore:
   has 'foo' => (lazy_build => 1);
   #is the same as
   has 'foo' => (lazy => 1, required => 1, predicate => 'has_foo', clearer => 'clear_foo', builder => '_build_foo);
   # or
   has 'foo' => (lazy => 1, required => 1, predicate => 'has_foo', clearer => 'clear_foo', default => sub{shift->_build_foo});

The reason for the different naming of the builder is that the builder method is a private method while the clearer and predicate methods are public methods.

NOTE: This means your class should provide a method whose name matches the value of the builder part, in this case _build__foo or _build_foo.

should_coerce
Returns true if this meta-attribute should perform type coercion.

should_auto_deref
Returns true if this meta-attribute should perform automatic auto-dereferencing.

NOTE: This can only be done for attributes whose type constraint is either ArrayRef or HashRef.

has_trigger
Returns true if this meta-attribute has a trigger set.

trigger
This is a CODE reference which will be executed every time the value of an attribute is assigned. The CODE ref will get two values, the invocant and the new value. This can be used to handle basic bi-directional relations.

documentation
This is a string which contains the documentation for this attribute. It serves no direct purpose right now, but it might in the future in some kind of automated documentation system perhaps.

has_documentation
Returns true if this meta-attribute has any documentation.

applied_traits
This will return the ARRAY ref of all the traits applied to this attribute, or if no traits have been applied, it returns undef.

has_applied_traits
Returns true if this meta-attribute has any traits applied.


BUGS

All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.


AUTHOR

Stevan Little <stevan@iinteractive.com>

Yuval Kogman <nothingmuch@woobling.com>


COPYRIGHT AND LICENSE

Copyright 2006-2008 by Infinity Interactive, Inc.

http://www.iinteractive.com

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

 Moose::Meta::Attribute - The Moose attribute metaclass