MacPerl used here
    

MacPerl: A Little Gem

by Rich Morin

SunExpert Magazine (October 1997; Volume 8, Number 10)


In Summer 1991, I was called by the Swiss army to do my compulsory 17 weeks of military basic training. While my duties were not unduly harsh, I soon experienced withdrawal symptoms from my programming habits and therefore started looking for a programming project to do on weekends. Since I had ported gawk to the Macintosh the preceding winter, and recently had become interested in Perl, I decided to attempt a port of Perl.

Casting Perl before Macintoshes
Matthias Neeracher, The Perl Journal
Volume 1, Issue 2, Summer 1996

Although MacPerl has been around for several years, many Perl aficionados (even those who regularly use and/or administer Macintosh systems) have never given it a try. This is very unfortunate; MacPerl is an extremely handy tool to have in the Mac OS environment. Perl has pretty much replaced awk as the "Swiss Army knife" of UNIX systems; thus, it might behoove you to examine this "Swiss Army port" of Perl for the Mac.

What is Perl?

Perl is an interpreted scripting language, based on bits of syntax from awk, sed, tr, and even Basic-Plus. It is capable of performing a wide range of functions, including many that could otherwise be done by combinations of UNIX commands. It is also able, however, to access low-level system functions (e.g., networking primitives), support complex data structures and object-oriented programming methods, and generally function as a modern, powerful programming language.

Because of Perl's scripting roots, it is very common for a Perl script to be orders of magnitude shorter than the equivalent C/C++ program. Features such as hashes (associative arrays), globbing (file name expansion), regular expressions, and very loose type rules work together to make Perl into a very powerful language.

For more information, visit The Perl Institute's web site (www.perl.org). While you're there, follow the link to The Perl Journal; it's a great little publication and I predict that it will grow rapidly as Perl matures. If you get seriously interested in Perl, be sure to obtain the definitive "Camel" and "Llama" books, published by O'Reilly and Associates (www.oreilly.com):

Learning Perl, 2nd edition (aka "the Llama book")
Schwartz and Christiansen
O'Reilly, 1997, ISBN 1-56592-284-0

Programming Perl, 2nd edition (aka "the Camel book")
Wall, Christiansen, & Schwartz
O'Reilly, 1996, ISBN 1-56592-149-6

O'Reilly also sponsors the "O'Reilly Perl Conference". Next month's I/Opener, in fact, will be an informal report from this year's conference (August 19-21, 1997; San Jose, CA). I expect the conference to be both interesting and informative. I'm looking forward to attending (next week, as I write this) and writing it up for you...

What is MacPerl?

Under UNIX, Perl's ability to emulate other commands is simply a pleasant side-effect of the language's overall capabilities. Under Mac OS, however, it can be a real life saver. Lacking any sort of convenient scripting facilities (MPW is not generally available and I don't count either AppleScript or HyperCard for much...), the Mac OS can be a real annoyance when a large, related set (i.e., a batch) of actions is needed. Although Perl is somewhat more verbose than the UNIX shell, it is acceptably ters [sic] and very functional.

Let's say you need to scan a directory tree, making copies of all the C or HTML files. Using the Mac OS Finder, this could take hours of tedious, error-prone labor. With MacPerl, however, it becomes a trivial scripting task. Using some of MacPerl's other features, you can do automated retrieval of HTML files, creation of input files for spreadsheets and databases, packet filtering or routing, and many other useful tasks.

And, because MacPerl is optimized for use under Mac OS, you can use it to create "droplets", Perl applications that support the Mac OS "drag and drop" protocol. Your users may not (and certainly need not) even know that you are using Perl to create these applications, but they certainly will be happy to have them!

In summary, MacPerl is a very complete port of Perl 5 to Mac OS, augmented by a variety of Mac-specific features. It can be run in any of a variety of ways providing convenience for both programmers and users. Finally, because MacPerl scripts do not rely on external UNIX commands, they tend to be unusually portable.

Compatibility

MacPerl makes some effort to act in a "reasonable" manner, bearing in mind the differences between Mac OS and UNIX. For instance, MacPerl follows MPW's lead in its handling of newline (\n) characters. That is, it produces Carriage Returns (ASCII \015, the Mac OS end-of-line delimiter), rather than Line Feeds (ASCII \012). If you really need a line feed, of course, you can specify the octal code.

For obvious reasons, MacPerl supports neither the "fork" call nor the backquote mechanism (though some special cases, such as `pwd`, are emulated, for convenience). The "system" call is only supported if MPW or the ToolServer has been installed. This tends to produce a more "self-contained" style of coding than is normally found in UNIX-based Perl. But, because in Perl "There's More Than One Way To Do It" (TMTOWTDI), there is generally at least one way to do it in MacPerl.

Programming

Apple provides an extensive programming and batch execution environment known as Macintosh Programmer's Workshop (MPW). Inspired by both the Smalltalk "Workspace" and the UNIX command set, MPW is quite popular among Mac OS programmers. MPW is available from Apple; it is also included in some commercial development environments, including CodeWarrior (Metrowerks; www.metrowerks.com). If you have access to MPW, you may wish to look into MacPerl's MPW support features.

The rest of us, fortunately, can use MacPerl as a stand-alone Finder application. MacPerl provides a built-in text editor, works with external text editors, and has modes of operation ("droplets" and "runtimes") that work well within Mac OS. The Macintosh Toolbox modules provide access to a variety of Macintosh features, including Apple Events, the Open Scripting Architecture, QuickTime Components, and several Managers (File, Gestalt, Memory, Process, and Resource).

MacPerl's built-in text editor (based on Apple's TextEdit facility) is mostly a tool of last resort. It suffers from a 32KB limit on file size, doesn't really understand fixed-width fonts, and has no real support for programming. Basically, it is an embedded version of Apple's TeachText editor, with all of the limitations you would expect.

Consequently, most MacPerl programmers edit their code using "real" text editors such as Alpha or BBEdit. The Tcl-based Alpha editor, which many MacPerl programmers recommend quite highly, is available as shareware. Visit the Alpha Web page (www.cs.umd.edu/~keleher/alpha.html) for more information.

BBEdit, which I use on a regular basis, has many pleasant and valuable features (e.g., syntax-based text coloring, regular expression-based search and replacement facilities, etc.). In addition, explicit support features are available for MacPerl programming. Bare Bones Software (www.barebones.com) sells BBEdit 4.5 for under $120 MSRP (educational or cross-upgrade discounts may apply); they also distribute a reduced-functionality version ("BBEdit Lite") at no charge.

Most MacPerl programmers will want to keep their scripts around as text files, to allow them to be modified at need. If you are setting up an application for your users, however, you may wish to create a MacPerl "droplet". Droplets are editable, using the MacPerl editor, but mostly they act like Mac OS programs. For instance, if you drag a file to the icon for a droplet, the droplet's Perl code will start up, using the file as its input. It is also possible to double-click a droplet and have it bring up a dialog requesting specific information on what you want it to do.

If you want to send a MacPerl program to someone who may not have a copy of MacPerl installed, you can have MacPerl create executable MacPerl applications ("runtime" files) which contain copies of the MacPerl runtime code. This is not a very efficient way to transmit Perl code, but it can be quite useful in some situations.

Support

Because MacPerl is closely tied to Perl, it benefits from all the standard Perl support facilities. For instance, many of the modules from the CPAN (Common Perl Archive Network) simply drop in and work. Again, visit www.perl.org to find out what's available.

There is a very active mailing list for MacPerl itself. List members are generally quite knowledgeable and helpful; some regularly do things to enhance MacPerl's utility. For instance, list members have ported and/or tried out many of the more popular CPAN modules (including a MacPerl port of LWP, the WWW access library). To join the MacPerl email list, send a note containing the body "subscribe" to mac-perl-request@iis.ee.ethz.ch.

Current versions of the MacPerl distribution are always available on the CPAN, mirrored from ftp://sunsite.cnlab-switch.ch/software/platform/macos/perl/. Both a MacPerl web site (www.ptf.com/macperl/) and a MacPerl CD-ROM are under construction. Visit the Web site for late-breaking news about MacPerl, including information on the status of the CD...