next up previous contents
Next: eval Up: A Few Perl Functions Previous: sleep   Contents

pack and unpack

The pack and unpack commands are primarily used for creating or reading binary structures to be compatible with, say, C programs. In addition, the unpack function can often be more convenient than the substr function (Section [*]) for dealing with some types of column-oriented data.

Both of the functions take, as their first argument, a string of characters which describe the format of the data to be packed or unpacked. The pack function takes a list of values to be packed as a second argument and returns a scalar character string containing the packed values. The unpack function takes a character string containing the values to be unpacked as a second argument, and returns a list of individual values extracted from the string. For a full list of the possible characters to use in the first argument to these functions, you can consult the online documentation (through perldoc -f pack); Table [*] lists a few of the commonly used codes. One caution about all but the ASCII and uuencoding formats is that these formats represent the native binary format for the data type on the computer on which perl is running -- if you create packed strings, they may not unpack properly on a different type of computer. Using them to pack and unpack data on the same computer should not cause any problems.

Table: Character codes for pack and unpack
Code Meaning Code Meaning
a ASCII string, null padded A ASCII string, blank padded
c Signed character C Unsigned character
d Double precision number f Single precision number
i Signed integer I Unsigned integer
l Signed long L Unsigned long
s Signed short S Unsigned short
u Uuencoded string @ Null-fill to absolute position
x Null byte X Back up a byte

When you're constructing a string of codes to use with these functions, you can optionally follow any of the codes with an integer repeat count, or an asterisk (*) to indicate as many repeats as possible. Each combination of a code and its optional count represents a chunk of data to either encode with pack or decode with unpack. To illustrate, consider the problem of breaking up the permissions from the ls command presented in Section [*]. We could extract the permissions and the filenames from each line of the ls -l output with the following call to unpack (assuming $_ contains a line of ls output):

     ($perms,$file) = unpack("a9\@54a*");
Similarly, the three triplets of permissions could be placed in an array @theperms with the following call to unpack:
     @theperms = unpack("a3" x 3,$perms);

The unpack function can be used, among other things, to decode uuencoded messages. Uuencoding is a technique which is sometimes used to encode binary files so that they can be safely emailed. (Binary files use all 8 bits of each byte of data, whereas ordinary text uses only 7 bits, so electronic mail is not suitable for sending binary files unless they are properly encoded.) The first line of a uuencoded file consists of the word begin followed by the octal code for the permission of the file and the file's name; this information should not be passed to the unpack function. Suppose we have a uuencoded binary file stored in the text file binary.uu, and we wish to extract the un-encoded form to the filename specified on the first line of the file. The following code would do the job:

open(FI,"<binary.uu") || die "Couldn't open binary.uu";
($begin,$perm,$name) = split(" ",<FI>);  #read the first line
$rest = join("",<FI>);                   #read the rest of the file
open(FI,">$name") || die "Couldn't open $name"  
print FI unpack("u*",$rest);        
By constructing an appropriate "begin" line, and using the pack function with the u* code, a similar approach could be used to create a uuencoded file suitable for sending via electronic mail.

next up previous contents
Next: eval Up: A Few Perl Functions Previous: sleep   Contents
Phil Spector 2002-10-18