Class::Accessor::Array - Generate accessors/constructor for array-based object


This document describes version 0.031 of Class::Accessor::Array (from Perl distribution Class-Accessor-Array), released on 2017-08-27.


In lib/Your/

 package Your::Class;
 use Class::Accessor::Array {
     # constructor => 'new',
     accessors => {
         foo => 0,
         bar => 1,

In code that uses your class:

 use Your::Class;

 my $obj = Your::Class->new;


 my $obj = Your::Class->new(foo => 1, bar => 2);

$obj is now:

 bless([1980, 12], "Your::Class");

To subclass, in lib/Your/

 package Your::Subclass;
 our @ISA = qw(Your::Class);
 use Class::Accessor::Array {
     accessors => {
         baz => 2,


This module is a builder for array-backed classes.

You can change the constructor name from the default new using the constructor parameter.

Currently the built constructor does not accept parameters to set the attributes, e.g.:

 my $obj = Your::Class->new(foo=>1, bar=>2); # not supported

You have to set the attributes manually:

 # supported
 my $obj = Your::Class->new;

If you subclass from another class that uses Class::Accessor::Array, you must make sure that: 1) the parent class' constructor is new; 2) you choose attribute array indices that have not already been used (unless you deliberately want to share storage space with attributes existing in the parent class). Multiple inheritance is not supported.

Note that if you're looking to reduce memory storage usage, an object based on Perl array is not that much-more-space-efficient compared to the hash-based object. Try representing an object as a pack()-ed string instead using Class::Accessor::PackedString.


Please visit the project's homepage at


Source repository is at


Please report any bugs or feature requests on the bugtracker website

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.


Class::Accessor::PackedString and Class::Accessor::PackedString::Fields.

Other class builders for array-backed objects: Class::XSAccessor::Array, Class::Accessor::Array::Glob, Class::ArrayObjects, Object::ArrayType::New.


perlancar <>


This software is copyright (c) 2017, 2016 by

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