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

NAME

Class::MOP::Attribute - Attribute Meta Object

SYNOPSIS

  Class::MOP::Attribute->new('$foo' => (
      accessor  => 'foo',        # dual purpose get/set accessor
      predicate => 'has_foo'     # predicate check for defined-ness      
      init_arg  => '-foo',       # class->new will look for a -foo key
      default   => 'BAR IS BAZ!' # if no -foo key is provided, use this
  ));
  
  Class::MOP::Attribute->new('$.bar' => (
      reader    => 'bar',        # getter
      writer    => 'set_bar',    # setter     
      predicate => 'has_bar'     # predicate check for defined-ness      
      init_arg  => ':bar',       # class->new will look for a :bar key
      # no default value means it is undef
  ));

DESCRIPTION

The Attribute Protocol is almost entirely an invention of this module, and is completely optional to this MOP. This is because Perl 5 does not have consistent notion of what is an attribute of a class. There are so many ways in which this is done, and very few (if any) are easily discoverable by this module.

So, all that said, this module attempts to inject some order into this chaos, by introducing a consistent API which can be used to create object attributes.

METHODS

Creation

new ($name, ?%options)

An attribute must (at the very least), have a $name. All other %options are contained added as key-value pairs. Acceptable keys are as follows:

init_arg

This should be a string value representing the expected key in an initialization hash. For instance, if we have an init_arg value of -foo, then the following code will Just Work.

  MyClass->meta->construct_instance(-foo => "Hello There");

In an init_arg is not assigned, it will automatically use the value of $name.

default

The value of this key is the default value which Class::MOP::Class::construct_instance will initialize the attribute to.

NOTE: If the value is a simple scalar (string or number), then it can be just passed as is. However, if you wish to initialize it with a HASH or ARRAY ref, then you need to wrap that inside a CODE reference, like so:

  Class::MOP::Attribute->new('@foo' => (
      default => sub { [] },
  ));
  
  # or ...  
  
  Class::MOP::Attribute->new('%foo' => (
      default => sub { {} },
  ));  

If you wish to initialize an attribute with a CODE reference itself, then you need to wrap that in a subroutine as well, like so:

  Class::MOP::Attribute->new('&foo' => (
      default => sub { sub { print "Hello World" } },
  ));

And lastly, if the value of your attribute is dependent upon some other aspect of the instance structure, then you can take advantage of the fact that when the default value is a CODE reference, it is passed the raw (unblessed) instance structure as it's only argument. So you can do things like this:

  Class::MOP::Attribute->new('$object_identity' => (
      default => sub { Scalar::Util::refaddr($_[0]) },
  ));

This last feature is fairly limited as there is no gurantee of the order of attribute initializations, so you cannot perform any kind of dependent initializations. However, if this is something you need, you could subclass Class::MOP::Class and this class to acheive it. However, this is currently left as an exercise to the reader :).

The accessor, reader, writer, predicate and clearer keys can contain either; the name of the method and an appropriate default one will be generated for you, or a HASH ref containing exactly one key (which will be used as the name of the method) and one value, which should contain a CODE reference which will be installed as the method itself.

accessor

The accessor is a standard perl-style read/write accessor. It will return the value of the attribute, and if a value is passed as an argument, it will assign that value to the attribute.

NOTE: This method will properly handle the following code, by assigning an undef value to the attribute.

  $object->set_something(undef);
reader

This is a basic read-only accessor, it will just return the value of the attribute.

writer

This is a basic write accessor, it accepts a single argument, and assigns that value to the attribute. This method does not intentially return a value, however perl will return the result of the last expression in the subroutine, which returns in this returning the same value that it was passed.

NOTE: This method will properly handle the following code, by assigning an undef value to the attribute.

  $object->set_something();
predicate

This is a basic test to see if the value of the attribute is not undef. It will return true (1) if the attribute's value is defined, and false (0) otherwise.

clearer

This is the a method that will uninitialize the attr, reverting lazy values back to their "unfulfilled" state.

clone (%options)
initialize_instance_slot ($instance, $params)

Value management

set_value ($instance, $value)

Set the value without going through the accessor. Note that this may be done to even attributes with just read only accessors.

get_value ($instance)

Return the value without going through the accessor. Note that this may be done even to attributes with just write only accessors.

has_value ($instance)
clear_value ($instance)

Informational

These are all basic read-only value accessors for the values passed into new. I think they are pretty much self-explanitory.

name
accessor
reader
writer
predicate
clearer
init_arg
is_default_a_coderef
default (?$instance)

As noted in the documentation for new above, if the default value is a CODE reference, this accessor will pass a single additional argument $instance into it and return the value.

slots

Returns a list of slots required by the attribute. This is usually just one, which is the name of the attribute.

Informational predicates

These are all basic predicate methods for the values passed into new.

has_accessor
has_reader
has_writer
has_predicate
has_clearer
has_init_arg
has_default

Class association

associated_class
attach_to_class ($class)
detach_from_class

Attribute Accessor generation

accessor_metaclass
associate_method
associated_methods
install_accessors

This allows the attribute to generate and install code for it's own accessor/reader/writer/predicate methods. This is called by Class::MOP::Class::add_attribute.

This method will call process_accessors for each of the possible method types (accessor, reader, writer & predicate).

process_accessors ($type, $value)

This takes a $type (accessor, reader, writer or predicate), and a $value (the value passed into the constructor for each of the different types). It will then either generate the method itself (using the generate_*_method methods listed below) or it will use the custom method passed through the constructor.

remove_accessors

This allows the attribute to remove the method for it's own accessor/reader/writer/predicate/clearer. This is called by Class::MOP::Class::remove_attribute.

Introspection

meta

This will return a Class::MOP::Class instance which is related to this class.

It should also be noted that Class::MOP will actually bootstrap this module by installing a number of attribute meta-objects into it's metaclass. This will allow this class to reap all the benifits of the MOP when subclassing it.

AUTHORS

Stevan Little <stevan@iinteractive.com>

Yuval Kogman <nothingmuch@woobling.com>

COPYRIGHT AND LICENSE

Copyright 2006 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.