David E. Wheeler

NAME

Class::Meta::Attribute - Class::Meta class attribute introspection

SYNOPSIS

  # Assuming MyApp::Thingy was generated by Class::Meta.
  my $class = MyApp::Thingy->class;
  my $thingy = MyApp::Thingy->new;

  print "\nAttributes:\n";
  for my $attr ($class->attributes) {
      print "  o ", $attr->name, " => ", $attr->call_get($thingy), $/;
      if ($attr->authz >= Class::Meta::SET && $attr->type eq 'string') {
          $attr->call_get($thingy, 'hey there!');
          print "    Changed to: ", $attr->call_get($thingy) $/;
      }
  }

DESCRIPTION

An object of this class describes an attribute of a class created by Class::Meta. It includes metadata such as the name of the attribute, its data type, its accessibility, and whether or not a value is required. It also provides methods to easily get and set the value of the attribute for a given instance of the class.

Class::Meta::Attribute objects are created by Class::Meta; they are never instantiated directly in client code. To access the attribute objects for a Class::Meta-generated class, simply call its class method to retrieve its Class::Meta::Class object, and then call the attributes() method on the Class::Meta::Class object.

INTERFACE

Instance Methods

name

  my $name = $attr->name;

Returns the name of the attribute.

type

  my $type = $attr->type;

Returns the name of the attribute's data type. Typical values are "scalar", "string", and "boolean". See Class::Meta for a complete list.

desc

  my $desc = $attr->desc;

Returns a description of the attribute.

label

  my $label = $attr->label;

Returns a label for the attribute, suitable for use in a user interface. It is distinguished from the attribute name, which functions to name the accessor methods for the attribute.

package

  my $package = $attr->package;

Returns the package name of the class that attribute is associated with.

view

  my $view = $attr->view;

Returns the view of the attribute, reflecting its visibility. The possible values are defined by the following constants:

Class::Meta::PUBLIC
Class::Meta::PRIVATE
Class::Meta::PROTECTED

context

  my $context = $attr->context;

Returns the context of the attribute, essentially whether it is a class or object attribute. The possible values are defined by the following constants:

Class::Meta::CLASS
Class::Meta::OBJECT

authz

  my $authz = $attr->authz;

Returns the authorization for the attribute, which determines whether it can be read or changed. The possible values are defined by the following constants:

Class::Meta::READ
Class::Meta::WRITE
Class::Meta::RDWR
Class::Meta::NONE

default

  my $default = $attr->default;

Returns the default value for a new instance of this attribute. Since the default value can be determined dynamically, the value returned by default() may change on subsequent calls. It all depends on what was passed for the default parameter in the call to add_attribute() on the Class::Meta object that generated the class.

call_get

  my $value = $attr->call_get($thingy);

This method calls the "get" accessor method on the object passed as the sole argument and returns the value of the attribute for that object. Note that it uses a goto to execute the accessor, so the call to call_set() itself will not appear in a call stack trace.

call_set

  $attr->call_set($thingy, $new_value);

This method calls the "set" accessor method on the object passed as the first argument and passes any remaining arguments to assign a new value to the attribute for that object. Note that it uses a goto to execute the accessor, so the call to call_set() itself will not appear in a call stack trace.

DISTRIBUTION INFORMATION

This file was packaged with the Class-Meta-0.14 distribution.

BUGS

Please report all bugs via the CPAN Request Tracker at http://rt.cpan.org/NoAuth/Bugs.html?Dist=Class-Meta.

AUTHOR

David Wheeler <david@kineticode.com>

SEE ALSO

Other classes of interest within the Class::Meta distribution include:

Class::Meta
Class::Meta::Class
Class::Meta::Method
Class::Meta::Constructor
Class::Meta::Type

COPYRIGHT AND LICENSE

Copyright (c) 2002-2004, David Wheeler. All Rights Reserved.

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