MacPerl: There's More than One Way to Do it

By: Vicki Brown

The year was 1995...

I had returned, some 6 months previously, to Apple Computer, after being away for nearly two years. Unlike my previous stint at Apple, this time my desktop machine did not run Unix. That was OK; I like the Macintosh. This time, my job was called "Quality Lead", which meant that I went to a lot of meetings. It did not mean I was writing much code myself, however, and I was beginning to get bored.

Then the Code Coverage Metrics were dropped in my lap. The Metrics were comprised of an enormous quantity of test and code coverage data, produced by a program that usually ran on Unix. Thanks to our contract with the vendor, the data was also being exported (dumped, actually) in a format suitable for importing into a spreadsheet, such as Microsoft Excel, on a Macintosh. All I had to do was massage the raw data into something interesting and useful to present to the weekly managers meetings.

There were many choices for how I could approach the task. Several people suggested that I simply import the data into Excel, munge it around by hand, and print the report. Every week. That idea, needless to say, was abandoned without so much as a moment's serious consideration on my part.

The second solution presented was that I use Excel's macro language to subset and massage the data into something more usable. I had used Excel's macro scripting capabilities before, at my previous (between-Apples) job. I knew it could be done... I also knew that I didn't want to do it.

If you've never worked with Excel macros on a Macintosh, you may not readily understand. Excel macros can accomplish amazing things, but they were not a "real" programming language in my eyes (the macros I had used were in Excel 4; pre-Visual Basic). Language aside, Excel macros are also Excel specific. They were not the type of experience that I thought would look good on a resume'.

And, at least on a Macintosh, they run in real time. Almost, but not quite, interactively, as you sit and watch. That is, while the macros run, the machine is at their mercy. Having worked with Excel before (on legacy files), I knew that Excel could do what I needed. But it wasn't powerful enough, it wasn't flexible enough, and it couldn't run in batch mode in the background while I did something else. So that idea was rejected as well.

The third choice was Unix. I had been working with Unix since 1983. I was experienced in awk and shell programming. I had an account on the company Unix system. I could ftp the files over, run some scripts, then ftp the results back to my desktop Macintosh. There, I could stuff the information into spreadsheets suitable for printing and distributing. It would have worked. But it wasn't the clean solution I was really hoping for.

But maybe there was yet a fourth solution...

A friend had been telling me about Perl for 5 years or more. Given that awk and shell did everything I needed (or so I thought :-), I had been listening with only half an ear. But now, awk and shell weren't readily available on my desktop. I had heard that there was a Macintosh port of Perl. Learning Perl could do my resume no harm. Perhaps Perl was the answer.

I downloaded MacPerl (version 4.1.8, based on Perl 4.036), bought myself a copy of Programming Perl (the first edition) and began to learn Perl. It was surprisingly easy (recall that I was experienced already with shell, awk, and sed; I also knew C). Perl was also surprisingly powerful. In short, Perl was getting the job done.

In a fairly short time, I had a working script. Which, of course, I began adding to. What additional statistics did we want? Which additional data fields did we want to extract? How did we want the data presented? With Perl, it was all possible.

I could even tell MacPerl to save the results in Microsoft Excel format (with the appropriate Type and Creator IDs).

             &MacPerl'SetFileInfo("XCEL", "TEXT", "$report");

The resulting output could be double-clicked and would launch as an Excel spreadsheet! (Remember that this was Perl version 4. These days, we'd use the module syntax:
             MacPerl::SetFileInfo("XCEL", "TEXT", "$report");

but the results are the same).
Soon my script was over 1000 lines long; it was also over the 32K limit of MacPerl's TextEdit-based internal editor! I moved up to BBEdit (an excellent text and code editor for the Macintosh, from Bare Bones Software, http://www.barebones.com) and kept coding.

Realizing that I had something very useful on my hands, I improved and bulletproofed the script to be usable by more than just myself. It was soon in use by several members of my department and by other departments as well. They didn't even have to understand Perl; they just had to double-click the script. And I was thoroughly and permanently hooked on Perl (and MacPerl).

Some three and a half years have passed since the Fates made me Keeper of the Code Coverage Metrics. In that time, both Perl and MacPerl have grown, changed, and improved. Perl version 5 was released; MacPerl 5 was released to match it. Programming Perl (aka "The Camel Book") is in its second edition, covering Perl 5. MacPerl: Power and Ease, the first book written specifically about MacPerl, was published in May, 1998.

When Baiju Thakkar decided to start Perlmonth, he wanted to include a MacPerl column. As co-authors of MacPerl: Power and Ease, Chris Nandor and I felt remarkably suited to host that column. So, here we are.

Each month, we'll bring you something about MacPerl. If you didn't know that Perl could run on a Macintosh, we'll enlighten you. If you're already using MacPerl, we'll try to show you some things you hadn't known before.

If you're a MacPerl user, you like to write, and you have something to share, let us know. In the months to come, we'll be looking for guest columnists for this space. After all, Chris and I don't know all the answers. If you prefer to read, whether you're a current MacPerl user, prospective, or just wondering why anyone would use Perl on a Macintosh, we invite you to sit back and enjoy the column.

We welcome your ideas. Please send comments, suggestions, and column ideas to macperl@perlmonth.com.

References:
MacPerl, currently version 5.2.0r4, is available from the CPAN in the directory /ports/mac.
The MacPerl Pages, sponsored by Prime Time Freeware, bring you pointers to MacPerl, the mailing lists, online resources, offline resources, and more.
MacPerl: Power and Ease, Vicki Brown and Chris Nandor is published by Prime Time Freeware, 1998. ISBN 1-881957-32-2
Programming Perl, 2nd. ed, Larry Wall, et. al., is published by O'Reilly & Associates, Inc., 1996. ISBN 1-56592-149-6