1. Perl
  2. builtin functions
  3. here

pack function - packs data into binary format

The pack function packs the data into various binary formats.

my $packed = pack($format, @values);

The first argument specifies the binary data format.

After the second argument, specify list of the values.

The return value is the packed data.

Packed in signed 32 - bit binary format

As an example, to pack three integers into a signed 32-bit binary format:

my $packed = pack("l3", 7, 8, 9);

First, 7, 8 and 9 are Perl data. Pack this into a signed 32-bit binary format.

The symbol "l" means "signed 32-bit integer".

The 3 that follows it means "three values".

The format consists of "symbols representing binary format" and "length".

Overall, it means packing the numbers 7, 8 and 9 in binary format with three signed 32-bit integers in a row.

In this example, we are passing a list as an argument, but you can also pass array.

my @values = (7, 8, 9);
my $packed = pack("l3", @values);

Pack floating point into signed double binary format

Let's pack the three floating point numbers into a double precision floating point double binary format.

my $packed = pack("d3", 0.5, 1.2, 5.4);

Double-precision floating point numbers can be packed with the symbol "d".

To write binary data

To write binary data, set the filehandle to binary mode using binmode function and print Use the function.

# Packed binary data
my $packed = pack("l3", 7, 8, 9);

# Open file
open my $fh, '<', $file
  or die "Can't open $file:$!";

# Change to binary mode
binmode $fh;

# Write binary data
print $fh $data;

# Close file
close $fh;

To change endianness

The conversion from big endian to little endian can be done using "N" and "V". The following example is an example of an unsigned 32-bit integer.

# Big endian value to Perl value
my $value = unpack('N1', $value_big);

# Perl value to little endian
my $value_litte = pack('V1', $value);

How to restore packed data

Use the unpack function to restore the packed data.

List of formats

List of formats for the pack function.

a A string containing arbitrary binary data. It will be null padded.
A Text (ASCII) string. It will be filled with spaces.
Z A null-terminated (ASCIZ) string. Filled with nulls.
b b Bit string (ascending bit order within each byte, such as the vec function)
B Bit string (descending bit order within each byte)
h Hexadecimal string (low nibbles first)
H Hexadecimal string (high nibbles first)
c Signed char (8-bit) value
C Unsigned char (octet) value
W Unsigned char value (may exceed 255)
s Signed short (16-bit) value
S Unsigned short value
l Signed long (32-bit) value
L Unsigned long value
q Signed quad (64-bit) value
Q Unsigned 4x value
i Signed integer value (at least 32 bits, C int)
I Unsigned integer value
n Unsigned short (16 bits) in network (big endian) order
N Unsigned long (32 bits) in network (big endian) order
v Unsigned short (16 bits) in "VAX" (little endian) order
V Unsigned long (32 bits) in VAX (little endian) order
j Perl's internally signed integer value (IV)
J A Perl's internal unsigned integer value (UV)
f Native format single precision floating point number
d Native format double precision floating point number
F Native Perl Internal Floating Point Values (NV)
D Native format long-double precision float type
p Pointer to null-terminated string
P Pointer to structure (fixed length string)

Advantages of binary format

The advantage of the binary format is that it is smaller in size. Perl numbers are internally a structure called SVIt is represented by, and the size is larger by the existence of the field of the structure.

Another advantage is that it is placed in a contiguous area . Since 32bit is 4 bytes, 3 4 bytes are consecutive and 12 bytes can express 3 integers.

Data exchange with C language

C language arrays correspond to just the packed binary format. The following declaration has exactly the same data structure as the packed binary format.

int32_t values [3];

More details

For more information about the pack function, see the official documentation Introduction to pack and pack function.

Related Informatrion