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/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:
$obj
bless(do{\(my $o = "")}, "Your::Class")
After:
$obj->bar(34);
bless(do{\(my $o = join("", chr(1), pack("c", 34)))}, "Your::Class")
$obj->foo(1.2);
bless(do{\(my $o = join("", chr(1), pack("c", 34), chr(0), pack("f", 1.2)))}, "Your::Class")
$obj->bar(undef);
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", ], };
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.
Please visit the project's homepage at https://metacpan.org/release/Class-Accessor-PackedString-Set.
Source repository is at https://github.com/perlancar/perl-Class-Accessor-PackedString-Set.
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.
Class::Accessor::PackedString
perlancar <perlancar@cpan.org>
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.
To install Class::Accessor::PackedString::Set, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::Accessor::PackedString::Set
CPAN shell
perl -MCPAN -e shell install Class::Accessor::PackedString::Set
For more information on module installation, please visit the detailed CPAN module installation guide.