home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomeLearning Perl, 3rd EditionSearch this book

16.2. Manipulating Data with pack and unpack

When storing data into a DBM file (or in one of the other types of databases we'll see in this chapter), you may need to store more than one item under a single key. And sometimes you'll need to be able to prepare some information to be sent over a network connection or to a system-level function, or to decode it upon arrival. That's why Perl has the pack and unpack functions.

The pack function takes a format string and a list of arguments and packs the arguments together to make a string. Here, we can pack three numbers of varying sizes into a seven-byte string using the formats c, s, and l (these might remind some folks of the words "char", "short", and "long"). The first number gets packed into one byte, the second into two bytes, and the third into four bytes, which explains why we say this is a seven-byte string:

my $buffer = pack("c s l", 31, 4159, 265359);

When you want the original list of items back, you can use the same format string with the unpack function:

my($char, $short, $long) = unpack("c s l", $buffer);

There are many different format letters available; some of these are the same on every machine (so they're useful for sending data over a network), while others depend upon how your machine likes to work with data (these are useful for interacting with your system's own data). See the perlfunc manpage for the latest list of format letters, as new ones are being added in every new version of Perl.

Whitespace may be used at will in a format string to improve readability, as we did in the previous example. For most format letters, you can follow the format letter with a number to indicate a number of times; that is, a format of "ccccccc" may be written more compactly as "c7". Instead of a number, you may follow the last format letter with a star (*), which means to use that format as many times as needed to use up the remaining items in the list (in pack) or to use up the rest of the string (in unpack). So a format of "c*" will either unpack a string into a list of small integers, or pack up those small integers to make a string. For some format letters, such as a, the number is not a repeat count; "a20" is a twenty-character ASCII string, padded with NUL characters as needed.

Library Navigation Links

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