Packing It In
Take a look at Perl’s confusing, but important pack and unpack functions.
The other authors of the wildly popular Learning Perl book and I dropped a few things between the second and third editions of the tome, simply because we wanted to make room for a few more relevant topics. As Perl has matured, Perl users migrated from being primarily system administrators to traditional developers, authoring complete mission-critical applications, such as some of the code behind many of the websites that you visit frequent. One of the sections that didn’t make the cut was the section on the confusing, but important pack and unpack functions. This month, let’s take a look at this small corner of the Perl universe.
Bits and Pieces
The pack function turns Perl-managable data (numbers and strings) into a sequence of bits that might make sense to some external application. unpack generally goes in the other direction, taking a bag-of-bits from some hostile, real-world interface, and turning the parade of one’s and zeroes into nice strings and numbers for further processing.
The number of options for pack and unpack is dizzying. In fact, as I was researching this article, I realized that I hadn’t read the related documentation for a few Perl releases, and it seems that they’ve snuck in about twice as many features as when I last looked. Too bad a pack format isn’t quite Turing-complete, although I’m happy they aren’t self-aware, as regular expressions seem to have become.