Chapter 19. Structure Conversion

pack template, list

Packs the values in list into a sequence of bytes, using the specified template. Returns this sequence as a string.

unpack template, expr

Unpacks the sequence of bytes in expr into a list, using template.

template is a sequence of characters as follows:

a / A

Byte string, null-/space-padded

b / B

Bit string in ascending/descending order

c / C

Signed/unsigned byte value

d / D

Native double/long double

f / F

Native float/Perl internal float

h / H

Hex string, low/high nybble first

i / I

Signed/unsigned integer value

j / J

Perl internal integer/unsigned

l / L

Signed/unsigned long value

n / N

Short/long in network (big endian) byte order

p / P

Pointer to a null-terminated/fixed-length string

q / Q

Signed/unsigned quad value

s / S

Signed/unsigned short value

u / U

Uuencoded string/Unicode UTF-8 character code

v / V

Short/long in VAX (little endian) byte order

w

A BER compressed integer

x / X

Null byte (skip forward)/Back up a byte

Z / @

Null-terminated string/null fill to position

The size of an integer, as used by i and I, depends on the system architecture. Nybbles, bytes, shorts, longs, and quads are always exactly 4, 8, 16, 32, and 64 bits respectively. Characters s, S, l, and L may be followed by a ! to signify native shorts and longs instead. x and X may be followed by a ! to specify alignment.

Each character, or group of characters between parentheses, may be followed by a decimal number, optionally between [ and ], that will be used as a repeat count; an asterisk ...

Get Perl Pocket Reference, 4th Edition 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.