David E. Wheeler


Class::Meta::AccessorBuilder - Perl style accessor generation


  package MyApp::TypeDef;

  use strict;
  use Class::Meta::Type;
  use IO::Socket;

  my $type = Class::Meta::Type->add( key     => 'io_socket',
                                     builder => 'default',
                                     desc    => 'IO::Socket object',
                                     name    => 'IO::Socket Object' );


This module provides the default accessor builder for Class::Meta. It builds standard Perl-style accessors. For example, an attribute named "io_socket" would have a single accessor method, io_socket.


Class::Meta::AccessorBuilder create three different types of accessors: read-only, write-only, and read/write. The type of accessor created depends on the value of the authz attribute of the Class::Meta::Attribute for which the accessor is being created.

For example, if the authz is Class::Meta::RDWR, then the method will be able to both read and write the attribute.

  my $value = $obj->io_socket;

If the value of authz is Class::Meta::READ, then the method will not be able to change the value of the attribute:

  my $value = $obj->io_socket;
  $obj->io_socket($value); # Has no effect.

And finally, if the value of authz is Class::Meta::WRITE, then the method will not return the value of the attribute (why anyone would want this is beyond me, but I provide for the sake of completeness):

  my $value = $obj->io_socket;  # Always returns undef.

Data Type Validation

Class::Meta::AccessorBuilder uses all of the validation checks passed to it to validate new values before assigning them to an attribute. It also checks to see if the attribute is required, and if so, adds a check to ensure that its value is never undefined. It does not currently check to ensure that private and protected methods are used only in their appropriate contexts, but may do so in a future release.

Class Attributes

If the context attribute of the attribute object for which accessors are to be built is Class::Meta::CLASS, Class::Meta::AccessorBuilder will build accessors for a class attribute instead of an object attribute. Of course, this means that if you change the value of the class attribute in any context--whether via a an object, the class name, or an an inherited class name or object, the value will be changed everywhere.

For example, for a class attribute "count", you can expect the following to work:

  my $count = MyApp::Custom->count; # Returns 10.
  my $obj = MyApp::Custom->new;
  $count = $obj->count;             # Returns 10.

  $count = $obj->count;             # Returns 22.
  my $count = MyApp::Custom->count; # Returns 22.

  $count = $obj->count;             # Returns 35.
  my $count = MyApp::Custom->count; # Returns 35.

Currently, class attribute accessors are not designed to be inheritable in the way designed by Class::Data::Inheritable, although this might be changed in a future release. For now, I expect that the current simple approach will cover the vast majority of circumstances.

Private and Protected Attributes

Any attributes that have their view attribute set to Class::Meta::Private or Class::Meta::Protected get additional validation installed to ensure that they're truly private and protected. This includes when they are set via parameters to constructors generated by Class::Meta. The validation is performed by checking the caller of the accessors, and throwing an exception when the caller isn't the class that owns the attribute (for private attributes) or when it doesn't inherit from the class that owns the attribute (for protected attributes).

As an implementation note, this validation is performed for parameters passed to constructors created by Class::Meta by ignoring looking for the first caller that isn't Class::Meta::Constructor:

  my $caller = caller;
  # Circumvent generated constructors.
  for (my $i = 1; $caller eq 'Class::Meta::Constructor'; $i++) {
      $caller = caller($i);

This works because Class::Meta::Constructor installs the closures that become constructors, and thus, when those closures call accessors to set new values for attributes, the caller is Class::Meta::Constructor. By going up the stack until we find another package, we correctly check to see what context is setting attribute values via a constructor, rather than the constructor method itself being the context.

This is a bit of a hack, but since Perl uses call stacks for checking security in this way, it's the best I could come up with. Other suggestions welcome. Or see Class::Meta::Type to create your own accessor generation code


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


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


David Wheeler <david@kineticode.com>



This class contains most of the documentation you need to get started with Class::Meta.


This module generates affordance style accessors.


This class manages the creation of data types.


This class manages Class::Meta class attributes, most of which will have generated accessors.


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.