template is a sequence of characters as follows:
| Byte string, null-/space-padded |
| Bit string in ascending/descending order |
| Signed/unsigned byte value |
| Native double/long double |
| Native float/Perl internal float |
| Hex string, low/high nybble first |
| Signed/unsigned integer value |
| Perl internal integer/unsigned |
| Signed/unsigned long value |
| Short/long in network (big endian) byte order |
| Pointer to a null-terminated/fixed-length string |
| Signed/unsigned quad value |
| Signed/unsigned short value |
| Uuencoded string/Unicode UTF-8 character code |
| Short/long in VAX (little endian) byte order |
| A BER compressed integer |
| Unsigned character value |
| Null byte (skip forward)/Back up a byte |
| Null-terminated string |
| Null fill or truncate to absolute/relative 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 (*
) specifies all remaining arguments. A template between [
and ]
is a repeat count equal to the length of the packed template. <
(little-endian) and >
(big-endian) can be used to force a specific byte order on a character or group.
Starting the template with U0
forces the result to be Unicode UTF-8, C0
forces bytes. Default is UTF-8.
If a format is preceded with %n, unpack returns an n-bit checksum instead. n defaults to 16.
Whitespace may be included in the template for readability, and a #
character may be used to introduce comments.
A special case is a numeric character code followed by a slash and a string character code, e.g., C/a.
Here the numeric value determines the length of the string item.
q
and Q
are only available if Perl has been built with 64-bit support. D
is only available if Perl has been built to support long doubles.