SPVM::Packer(3)
π SPVM::Packer β Pack and Unpack Operations π
okay so bestie, welcome to SPVM::Packer β it's a class in SPVM that has methods for pack and unpack operations. That's the whole vibe. (This is a page from User Contributed Perl Documentation, fyi, so you know exactly which little corner of the perl universe you're standing in π«Ά)
πΈ Description πΈ
The Packer class in SPVM has methods for pack and unpack operations. No cap. It takes your objects, smushes them into binary, and can pull them right back out again. Iconic behaviour, honestly.
β¨ Usage β¨
here's the whole thing in action, literally:
use Packer;
my $packer = Packer->new;
{
my $objects = [(object)1, 2L, 0.5, [1, 2], "abc"];
my $binary = $packer->pack("lqdl2a128", $objects);
my $objects_unpack = $packer->unpack("lqdl2a128", $binary);
}
{
my $objects = [(object)[1, 2, 3]];
my $binary = $packer->pack("l*", $objects);
my $objects_unpack = $packer->unpack("l*", $binary);
}
see how the template "lqdl2a128" goes in and comes back out the same way? that's the whole point, bestie π
π Class Methods π
π static method new : Packer ();
Creates a new Packer object. That's it. That's the method. Slay. β¨
π¦ Instance Methods π¦
π pack
static method pack : string ($template : string, $objects : object[]);
Converts the objects $objects to a binary data according to the template $template, and returns it. so you hand it a pile of objects and a template, and it hands you back a string of raw bytes. main character energy fr.
π Template Syntax π
A template is a string. It consist of specifier parts.
TEMPLATE := "SpecifierPart1SpecifierPart2SpecifierPartN"
A specifier part is consist of a speficier, an endian, and a length. An endian and a length are optional.
SpecifierPart := Specifier[Endian][Length]
Here is the list of specifiers. (yes, they're giving very much a la carte menu π)
aβstringβ string. It is null padded inpackmethod.hβstringβ A hex string (low nybble first)Hβstringβ A hex string (high nybble first)cβByteorbyte[]β Signed 8-bit integersCβByteorbyte[]β Unsigned 8-bit integerssβShortorshort[]β Signed 16-bit integersSβShortorshort[]β Unsigned 16-bit integerslβIntorint[]β Signed 32-bit integersLβIntorint[]β Unsigned 32-bit integersqβLongorlong[]β Signed 64-bit integersQβLongorlong[]β Unsigned 64-bit integersfβFloatorfloat[]β 32-bit floating point numbers.dβDoubleordouble[]β 64-bit floating point numbers.
(yes, the left column is the specifier, the middle is the Types, and the right one is an output binary in pack or an input binary in unpack π)
An endian is big-endian > or little-endian >. (that's exactly how the doc spells it, bestie β two >s, don't come for me π)
- If big-endian is specified, the binary data is converted from big-endian to system-endian in
packmethod, or from system-endian to big-endian inunpackmethod. - If little-endian is specified, the binary data is converted from little-endian to system-endian in
packmethod, or from system-endian to little-endian inunpackmethod.
A length must be a positive integer or a wildcard *. If a length or a wildcard is specified, the type (such as Int) becomes a corresponding array type (such as int[]). so like, l2 gives you int[] with two of them, and l* gives you the whole wildcard situation. okay that last bit was me explaining the vibe, the doc just says what it says π
π Exceptions π
okay so pack is a little dramatic. it will throw at you. here's the full list of ways to upset it:
- If template syntax is invalid, an exception is thrown.
- The template
$templatemust be defined. Otherwise an exception is thrown. - The objects
$objectsmust be defined. Otherwise an exception is thrown. - The length of the specifiers in the template
$templatemust be less than or equal to the lenght of the objects$objects. Otherwise an exception is thrown. - The type of the element in the objects
$objectsis invalid, an exception is thrown.
π unpack
static method unpack : object[] ($template : string, $binary : string);
Converts the binary data $binary to the objects $objects according to the template $template, and returns it. so it's the reverse glow-up of pack β bytes go in, objects come out, everyone cheers π
See "pack" method about templates. same menu, same rules, same everything.
π Exceptions π
once again, the drama. unpack has its own list:
- If template syntax is invalid, an exception is thrown.
- The template
$templatemust be defined. Otherwise an exception is thrown. - The binary data
$binarymust be defined. Otherwise an exception is thrown. - The current offset
$binary_offsetplus (the size$size* the length$lengthof the specifier) must be less than the length($binary_length) of the binary data$binary. Otherwise an exception is thrown.
π§ Copyright & License π§
Copyright (c) 2024 Yuki Kimoto
MIT License
and that's the whole page, bestie π β brought to you by perl v5.42.2, dated 2026-02-03, filed under SPVM::Packer(3). go pack something iconic πβ¨
π¦ where this page comes from
- alpine 3.24: spvm-doc 0.990125-r0 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms