The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Class::Accessor::PackedString::Set - Like Class::Accessor::PackedString, but store attributes as they are set


This document describes version 0.001 of Class::Accessor::PackedString::Set (from Perl distribution Class-Accessor-PackedString-Set), released on 2017-10-15.


In lib/Your/

 package Your::Class;
 use Class::Accessor::PackedString::Set {
     # constructor => 'new',
     accessors => [
         foo => "f",
         bar => "c",

In code that uses your class:

 use Your::Class;

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

$obj is now:

 bless(do{\(my $o = "")}, "Your::Class")



$obj is now:

 bless(do{\(my $o = join("", chr(1), pack("c", 34)))}, "Your::Class")



$obj is now:

 bless(do{\(my $o = join("", chr(1), pack("c", 34), chr(0), pack("f", 1.2)))}, "Your::Class")



$obj is now:

 bless(do{\(my $o = join("", chr(0), pack("f", 1.2)))}, "Your::Class")

To subclass, in lib/Your/

 package Your::Subclass;
 use parent 'Your::Class';
 use Class::Accessor::PackedString::Set {
     accessors => [
         baz => "a8",
         qux => "a8",


This module is a builder for classes that use string as memory storage backend. The string is initially empty when there are no attributes set. When an attribute is set, string will be appended with this data:

 | size        | description                        |
 | 1 byte      | index of attribute                 |
 | (pack size) | attribute value, encoded by pack() |

When another attribute is set, string will be further appended. When an attribute is unset (undef'd), its entry will be removed in the string.

This module is similar to Class::Accessor::PackedString. Using string (of pack()-ed data) is useful in situations where you need to create many (e.g. thousands+) objects in memory and want to reduce memory usage, because string-based objects are more space-efficient than the commonly used hash-based objects. Unlike in Class::Accessor::PackedString, space is further saved by only storing set attributes and not unset attributes. This particularly saves significant space if you happen to have many attributes with usually only a few of them set.

The downsides are: 1) you have to predeclare all the attributes of your class along with their types (pack() templates); 2) you can only store data which can be pack()-ed; 3) slower speed, because unpack()-ing and re-pack()-ing are done everytime an attribute is accessed or set.


There is a maximum of 256 attributes.


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.




perlancar <>


This software is copyright (c) 2017 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.