The Perl Advent Calendar needs more articles for 2022. Submit your idea today!


Class::Accessor::FactoryTyped - Accessors whose values come from a factory


version 1.100970


    package Person;
    use base 'Class::Accessor::FactoryTyped';

        person_name    => 'name',
        person_address => 'address',


This module generates accessors for your class in the same spirit as Class::Accessor does. While the latter deals with accessors for scalar values, this module provides accessor makers for arrays, hashes, integers, booleans, sets and more.

As seen in the synopsis, you can chain calls to the accessor makers. Also, because this module inherits from Class::Accessor, you can put a call to one of its accessor makers at the end of the chain.

The accessor generators also generate documentation ready to be used with Sub::Documentation.



        foo => 'phooey',
        bar => [ qw(bar1 bar2 bar3) ],
        baz => {
            slot => 'foo',
            comp_mthds => [ qw(bar baz) ]
        fob => [
                slot       => 'dog',
                comp_mthds => 'bark',
                slot       => 'cat',
                comp_mthds => 'miaow',

This behaves a lot like Class::Accessor::Complex's mk_object_accessors(), but the types of objects - that is, their class names - that the generated accessors can take aren't given statically, but are determined by asking a factory.

The factory class name must be the first argument. The class indicated should be a subclass of Class::Factory::Enhanced.

The following argument is an array which should contain pairs of class => sub-argument pairs. The sub-arguments are parsed like this:

Hash Reference

See baz() above. The hash should contain the following keys:


The name of the instance attribute (slot).


A string or array reference, naming the methods that will be forwarded directly to the object in the slot.

Array Reference

As for String, for each member of the array. Also works if each member is a hash reference (see fob() above).


The name of the instance attribute (slot).

For each slot x, with forwarding methods y() and z(), the following methods are created:


A get/set method, see * below.


Forwarded onto the object in slot x, which is auto-created via new() if necessary. The new(), if called, is called without arguments.


As for y.

So, using the example above, a method, foo(), is created, which can get and set the value of those objects in slot foo, which will generally contain an object of the type the factory, in this case My::Factory, uses for the object type baz. Two additional methods are created named bar() and baz() which result in a call to the bar() and baz() methods on the Baz object stored in slot foo.

Apart from the forwarding methods described above, mk_object_accessors() creates methods as described below, where * denotes the slot name.


If the accessor is supplied with an object of an appropriate type, will set set the slot to that value. Else, if the slot has no value, then an object is created by calling new() on the appropriate class, passing in any supplied arguments.

The stored object is then returned.

*_clear, clear_*

Removes the object from the accessor.


Like mk_factory_typed_accessors() except creates array accessors with all methods like those generated by Class::Accessor::Complex's mk_array_accessors().


See perlmodinstall for information and options on installing Perl modules.


No bugs have been reported.

Please report any bugs or feature requests through the web interface at


The latest version of this module is available from the Comprehensive Perl Archive Network (CPAN). Visit to find a CPAN site near you, or see

The development version lives at Instead of sending patches, please fork this project using the standard git and github infrastructure.


  Marcel Gruenauer <>


This software is copyright (c) 2007 by Marcel Gruenauer.

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