NAME
Class::Accessor::PackedString::Set - Like Class::Accessor::PackedString, but store attributes as they are set
VERSION
This document describes version 0.001 of Class::Accessor::PackedString::Set (from Perl distribution Class-Accessor-PackedString-Set), released on 2017-10-15.
SYNOPSIS
In lib/Your/Class.pm:
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")
After:
$obj->bar(34);
$obj
is now:
bless(do{\(my $o = join("", chr(1), pack("c", 34)))}, "Your::Class")
After:
$obj->foo(1.2);
$obj
is now:
bless(do{\(my $o = join("", chr(1), pack("c", 34), chr(0), pack("f", 1.2)))}, "Your::Class")
After:
$obj->bar(undef);
$obj
is now:
bless(do{\(my $o = join("", chr(0), pack("f", 1.2)))}, "Your::Class")
To subclass, in lib/Your/Subclass.pm:
package Your::Subclass;
use parent 'Your::Class';
use Class::Accessor::PackedString::Set {
accessors => [
@Your::Class::HAS_PACKED,
baz => "a8",
qux => "a8",
],
};
DESCRIPTION
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.
Caveats:
There is a maximum of 256 attributes.
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/Class-Accessor-PackedString-Set.
SOURCE
Source repository is at https://github.com/perlancar/perl-Class-Accessor-PackedString-Set.
BUGS
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Class-Accessor-PackedString-Set
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.
SEE ALSO
AUTHOR
perlancar <perlancar@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2017 by perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.