packTEMPLATE,LIST
This function 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 gives the order and type
of values, as follows:
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
(continued)
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
x
A null byte
X
Back up a byte
@
Null-fill to absolute position
Each character may optionally be followed by a number which 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 to use however many items are left. The
"a"
and
"A"
types gobble just one value,
but pack it as a string of length
count
, padding with nulls
or spaces as necessary. (When unpacking,
"A"
strips trailing
spaces and nulls, but
"a"
does not.) Real numbers (floats
and doubles) are in the native machine format only; due to the multiplicity of
floating formats around, and the lack of a standard network representation, no
facility for interchange has been made. This means that packed floating-point
data written on one machine may not be readable on another - even if both
use IEEE floating-point arithmetic (as the endian-ness of the memory
representation is not part of the IEEE spec). Also, Perl uses doubles
internally for all numeric calculation, and converting from double to float to
double will lose precision; that is,
unpack("f",
pack("f",$num))
will not in general equal
$num
.
This first pair of examples packs numeric values into bytes:
$out = pack "cccc", 65, 66, 67, 68; # $out eq "ABCD" $out = pack "c4", 65, 66, 67, 68; # same thing
This does a similar thing, with a couple of nulls thrown in:
$out = pack "ccxxcc", 65, 66, 67, 68; # $out eq "AB0円0円CD"
Packing your shorts doesn't imply that you're portable:
$out = pack "s2", 1, 2; # "1円0円2円0円" on little-endian # "0円1円0円2円" on big-endian
On binary and hex packs, the count refers to the number of bits or nybbles, not the number of bytes produced:
$out = pack "B32", "01010000011001010111001001101100"; $out = pack "H8", "5065726c"; # both produce "Perl"
The length on an
"a"
field applies only to one string:
$out = pack "a4", "abcd", "x", "y", "z"; # "abcd"
To get around that limitation, use multiple specifiers:
$out = pack "aaaa", "abcd", "x", "y", "z"; # "axyz" $out = pack "a" x 4, "abcd", "x", "y", "z"; # "axyz"
The
"a"
format does null filling:
$out = pack "a14", "abcdefg"; # "abcdefg0円0円0円0円0円0円0円"
This template packs a C
struct tm
record (at least on some
systems):
$out = pack "i9pl", gmtime, $tz, $toff;
The same template may generally also be used in the unpack function. If you want to join variable length fields with a delimiter, use the join function.
Note that, although all of our examples use literal strings as templates, there is no reason you couldn't pull in your templates from a disk file. You could, in fact, build an entire relational database system around this function.
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl Programming | Perl Cookbook ]