WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Perl · Intermediate · question 40 of 100

Explain how to use Perl’s ’pack’ and ’unpack’ functions for binary data manipulation.?

📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

Perl’s ’pack’ and ’unpack’ functions are used to convert data between binary strings and Perl variables. These functions are commonly used in situations where data needs to be converted between different systems or applications that use different data formats.

The ’pack’ function takes a list of values and packs them into a binary string according to a specified format string. For example, to pack two integers and a string into a binary string, we can use the following code:

my $packed_data = pack("LLZ*", $int1, $int2, $string);

In this example, the format string "LLZ*" specifies that we want to pack two unsigned long integers (each four bytes long) followed by a null-terminated string. The values to be packed are passed as arguments to the pack function. The resulting binary string is stored in the scalar variable $packed_data.

The ’unpack’ function does the reverse operation. It takes a binary string and unpacks it into a list of values according to a specified format string. For example:

my ($unpacked_int1, $unpacked_int2, $unpacked_string) = unpack("LLZ*", $packed_data);

In this example, the format string "LLZ*" is the same as in the previous example. The unpacked values are assigned to the scalar variables $unpacked_int1, $unpacked_int2, and $unpacked_string.

Here’s a more detailed explanation of the format string syntax:

The format string consists of several directives, each of which specifies a data type and its size. Directives can be combined to specify a complex data structure.

Some of the most commonly used directives are:

- ’c’ for a signed char integer (one byte)

- ’C’ for an unsigned char integer (one byte)

- ’s’ for a signed short integer (two bytes)

- ’S’ for an unsigned short integer (two bytes)

- ’l’ for a signed long integer (four bytes)

- ’L’ for an unsigned long integer (four bytes)

- ’f’ for a floating-point number (four bytes)

- ’d’ for a double-precision floating-point number (eight bytes)

- ’Z’ for a null-terminated string

Each directive can be followed by a number indicating the number of times it should be repeated. For example, the directive ’L4’ means "four unsigned long integers".

Some directives can be modified with special characters to specify the endianness and byte order of the data. For example, the directive ’V’ means "unsigned long in little-endian byte order" and the directive ’N’ means "unsigned long in big-endian byte order".

Overall, the ’pack’ and ’unpack’ functions are powerful tools for binary data manipulation in Perl. They provide a flexible and efficient way to exchange data between different systems and applications.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Perl interview — then scores it.
📞 Practice Perl — free 15 min
📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

All 100 Perl questions · All topics