Name

pack

Synopsis

pack template, list

Description

Takes a list of values and packs it into a binary structure, returning the string containing the structure. The template is a sequence of characters that give the order and type of values, as follows:

CharacterMeaning
a An ASCII string, will be null padded
A An ASCII string, will be space padded
b A bit string, low-to-high order (like vec())
B A bit string, high-to-low order
c A signed char value
C An unsigned char value
d A double-precision float in the native format
f A single-precision float in the native format
h A hexadecimal string, low nybble first
H A hexadecimal string, high nybble first
i A signed integer value
I An unsigned integer value
l A signed long value
L An unsigned long value
n A short in “network” (big-endian) order
N A long in “network” (big-endian) order
p A pointer to a string
P A pointer to a structure (fixed-length string)
s A signed short value
S An unsigned short value
v A short in “VAX” (little-endian) order
V A long in “VAX” (little-endian) order
u A uuencoded string
w A BER compressed integer
x A null byte
X Back up a byte
@ Null-fill to absolute position

Each character may optionally be followed by a number that gives a repeat count. Together the character and the repeat count make a field specifier. Field specifiers may be separated by whitespace, which will be ignored. With all types except a and A, the pack function will gobble up that many values from the list. Saying * for the repeat count means ...

Get Perl in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.