Perl in a Nutshell

Perl in a NutshellSearch this book
Previous: Reference: ordChapter 5
Function Reference
Next: Reference: package
 

pack

pack template, list

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
aAn ASCII string, will be null padded
AAn ASCII string, will be space padded
bA bit string, low-to-high order (like vec())
BA bit string, high-to-low order
cA signed char value
CAn unsigned char value
dA double-precision float in the native format
fA single-precision float in the native format
hA hexadecimal string, low nybble first
HA hexadecimal string, high nybble first
iA signed integer value
IAn unsigned integer value
lA signed long value
LAn unsigned long value
nA short in "network" (big-endian) order
NA long in "network" (big-endian) order
pA pointer to a string
PA pointer to a structure (fixed-length string)
sA signed short value
SAn unsigned short value
vA short in "VAX" (little-endian) order
VA long in "VAX" (little-endian) order
uA uuencoded string
wA BER compressed integer
xA null byte
XBack 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 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.

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.


Previous: Reference: ordPerl in a NutshellNext: Reference: package
Reference: ordBook IndexReference: package

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.