The MacPerl Development Environment

By: Vicki Brown

The Macintosh is well-known for its user interface: point-and-click, drag-and-drop, mouse and menus. Perl, on the other hand, is largely text based. You may thus be wondering whether Perl can take advantage of what the Macintosh has to offer... is there a reasonable Development Environment for MacPerl?

There is, although it may not be quite what you were hoping for. Unfortunately, there is no fancy drag-and-drop interface that would allow users to drag representations of Perl modules around on the screen, linking them together visually. Not yet, anyway. You still have to type a lot.

On the other hand, the Mac has a lot to offer. I've been using Unix since 1983 and I love vi (I've never felt compelled to learn emacs). Nevertheless, I've switched over to using the Macintosh for nearly all of my Perl development these days. I get syntax-based text coloring, drag-and-drop editing, double-click error browsing, point-and-click syntax checking, ease of use, ... and a backup copy of my work for free.

This month, I thought I'd give you a tour of the Mac's Perl development environment as I use it. I may not convince you to switch over, but it's worth a try (:-).

MacPerl with a command line

MacPerl comes in two flavors: the standalone MacPerl application and the MacPerl MPW tool. MPW [1] is Apple Computer's Macintosh Programmers Workbench, a command-line oriented shell/editor environment. Originally available only to registered developers, Apple has recently made MPW freely available for downloading and use by anyone.

A word of caution if you're used to Unix - MPW will remind you of Unix. You may find that this is just enough of a reminder to trip you when you least expect it. For example, MPW Perl scripts, like Unix scripts, can be made executable. Unlike the simple Unix

    #!/usr/bin/perl
prefix, however, MPW scripts must include a somewhat more cumbersome incantation:
    Perl -Sx "{0}" {"Parameters"}; Exit

Once you have MPW running, you can run Perl from the command line, create and run Perl scripts, and generally use Perl as you would expect to under Unix.

MPW cmd line

Keep in mind that things are different in the MPW environment: pathnames use colons; redirection, newline-escape, and other metacharacters use the option key... Perhaps most important, in MPW, the shell is also a (modeless) text editor. Commands are run by selecting and pressing Enter. The Return key simply adds a return character. You'll get used to it...

MPW cmd line

The MacPerl Application

If you don't feel the need for a command line, you may be happier using the standalone application. Windows in the MacPerl app are simply editor windows. Type in your Perl script, save the file, then run the script using the various menu commands, most of which can be found in the Script menu.

Script menu

Standard output and standard error output appear in a new window, the MacPerl Output window. If your script opens and writes to a file, of course, the output will go there.

There's even a simple menu item to run a single one-line Perl command, should you care to do so.

oneliner

The script that puts up the one-liner dialog is written in MacPerl, and is included in the distribution as an example of how to create dialogs.

Adding a Better Editor

Unfortunately, the editor that is included with the MacPerl Application is woefully simpleminded; it's based on the same editor model that was used for TeachText, Apple's original editor (created for writing simple README files).

This editor, known as TextEdit, has very few features: no line numbers, no drag-and-drop, no tabs. Just type, copy, cut, paste, and delete. Worse, it can only handle files with a maximum size of 32K. There must be a better way... and there is.

If you specify your Internet "editor" helper, using an application called Internet Config [2] (included with the MacPerl distribution), MacPerl will use that editor in place of its built-in editor. You'll even get a special editor menu to make editing your MacPerl code even easier.

Edit menu

A number of text editors are available for the Macintosh; features vary, however, depending on their expected use. Two of the most popular editors for editing MacPerl code are Alpha [3], a text editor written by Peter Keleher, and BBEdit [4], a text editor available from Bare Bones Software. Both editors provide line numbers, drag and drop editing, syntax-based text coloring, pattern-based Find and Replace, indentation control, extensions (plugins, filters, etc.), and more. Demo versions of each are available from the vendor, so you can "try before you buy". Choose the one you prefer.

My personal preference in Macintosh text editors is BBEdit. I've been using the commercial version of BBEdit for several years now. (Note that BBEdit Lite, a freeware version of BBEdit, has a reduced feature set. Many of the more powerful features that I use and mention here are not available in BBEdit Lite).

Recently, Bare Bones Software announced enhanced support for MacPerl by adding direct integration of MacPerl into BBEdit 5.1:

BBEdit 5.1 includes facilities to assist Perl script writers in developing and debugging Perl scripts. In addition, Perl may be used to enhance BBEdit's already-formidable text processing power: users may create "Perl filters" for processing text in open document windows, and can access frequently-used Perl scripts from a dedicated menu. Finally, web site developers can now use Perl in conjunction with BBEdit's automated HTML document-management tools for unprecedented flexibility in generating and maintaining Web content.

Working from Within the Editor

Once you have installed and specified a better text editor, you're well on your way to having a greatly improved interface for working with MacPerl. Just pull down the menu and edit your script in your chosen editor, then use the Script menu to syntax check and run it. But why stop there?

If you've chosen an editor with support for writing code, such as Alpha or BBEdit, you may do better to work from within the editor itself, using the MacPerl application only as an engine to run your MacPerl scripts. That's what I do.

sample.plx

BBEdit provides a Perl palette that allows me to syntax-check or run my code at the click of a button. If I have a syntax error (horrors!), BBEdit brings up the error in its error browser. I need only click on a given error, and the browser will show me the offending code, in context. Double click, and BBEdit opens the file for me, selecting the offending line, ready to edit.

error

Once you've worked with a "real" text editor with support for editing code, you'll wonder how you ever got along without it. My editor has "grep"-style (regular expression) pattern matching for Search and Replace, a function menu that automatically recognizes and includes functions in the current language, language-sensitive syntax coloration (keywords, comments, and strings), split-screen capability (edit a function in one pane; edit the code that calls it in another) and much more. I can even write Perl filters that I can access from within the editor, filtering the text in my current window.

POD Viewing with Shuck

As does any Perl distribution, MacPerl ships with oodles of online documentation. Considering all of the Mac Toolbox calls that have been ported, MacPerl may even ship with more documentation than most Perl distributions!

That documentation is readily accessible and easy to read, thanks to Shuck, the MacPerl Pod-reader application. Shuck can be used in several ways. Because POD is a standalone application, you can simply drop a POD file onto the Shuck icon.

Alternatively, you can access any of the standard documentation from the Help menu in the MacPerl application, or the Go menu in the Shuck application. I must confess that I find the menu entries far less cryptic than the standard Unix Perl documentation. Instead of remembering

  perldoc perlsyn
I simply select "Syntax" from the Help menu. When I want to look through the Frequently Asked Questions, I can browse the titles in the PerlFAQ submenu...

Finally, if I want to look up a particular Perl function or keyword, I need only highlight it in BBEdit and click "Find in Shuck" on BBEdit's Perl pallette; the appropriate Pod file will be opened to the precise location describing the function. (Other editors may have similar features).

I Still Want a Command Line, Sometimes...

Considering that I really like my text editor, and find it to be a very conducive environment for creating Perl code, I'm not very anxious to move back to MPW, even if I want to try some command line Perl. But sometimes, the command line is the very thing I need. Enter ToolServer, a standalone application that allows me to run MPW tools (binary executables) and scripts... without MPW. With ToolServer installed, I can even use the system(...) function from within my MacPerl scripts!

That's not all, however. With some minor additional configuration, BBEdit gives me integrated support for ToolServer as well. In fact, I can run MPW shell command (including perl) in a BBEdit window as if in the MPW shell... but without the overhead of another application, and without leaving my preferred editor environment.

To use ToolServer with MacPerl, be sure to copy the Perl startup file from your MPW Startup Items folder into the TS Startup Items folder! Also note that due to implementation details, calling system(...) does not work from within the integrated ToolServer environment. (Win some, lose some.)

Source Code Management

What more could I ask for? Well, a source code control system would be nice. Under Unix, I have a choice of SCCS or RCS. With MacPerl, I have... SourceServer.

SourceServer is another MPW standalone, integrated into BBEdit with version 5.1. I simply set up a project by selecting "New Project..." from the SourceServer menu, telling SourceServer which files are part of my "project". After that, it's easy. As I work with the code, I can check revisions in and out with the click of a mouse button, without ever leaving my development environment.

FTP

"OK", I hear you say. "That's great... but I deploy most of my applications on Unix...". Well, so do I; that's no problem either. BBEdit has integrated FTP support.

I can either open a file and edit it "directly" on the remote host (not really, but I'll explain in a minute). Or I can open a file on my Macintosh, edit it, syntax check it, test (all or part of the code), then save a copy to the FTP server. The latter provides me with an instant backup, and I know that the version on the Unix server is always up-to-date and tested.

Getting back to editing "directly" on the remote host... what actually happens is that BBEdit makes a temporary copy of the file on my Macintosh. When I save changes, BBEdit opens an FTP connection and copies the data to the server. But it looks and feels as if I'm actually editing on the server. In fact, the pathname in my edit window shows the Unix path.

I can save any number of bookmarks to various hosts or frequently accessed directories. When I need to change something... open, edit, save. It's almost like being there.

Does Anyone Print Listings Anymore?

I do. I like to keep a hard-copy backup and I also find that sometimes it's easier to work through a program when it's on paper. BBEdit allows me to print my code in color (if I use a color printer). Even on a grayscale printer, I get shading. I've chosen the colors for my comments, keywords, and strings carefully, to allow me to distinguish these even in shades of gray.

But supposing I want to publish my code on my web pages? Until recently, I would have had two choices: use a simple .txt file, or wrap the code in <PRE> tags in an HTML file. However, I've discovered a better way.

I use a cool commercial application called Terry Morse Myrmidon [5]. Myrmidon is an application that acts like a printer driver. I simply choose Myrmidon as I would any printer, configure the desired options in my "Page Setup" dialog, then select Print. Instead of paper, I get HTML files, complete with syntax coloring, headers, and everything else I would expect from printed output, all faithfully reproduced.

Are you convinced?

I hope so. If you have a Macintosh, I suggest you try using it in the way I have described. I think you'll get hooked on this powerful Perl development environment.

If you don't have a Macintosh, well, you could always get one :-)

References

[1] MPW, ToolServer, SourceServer, and are available from Apple Computer's MPW ToolZone.

[2] The Internet Configuration System was designed to centralize your Internet preferences in one convenient location. Internet Config 1.4 is bundled with the MacPerl distribution. The latest version (2.0) is available from Stairways Software Pty Ltd.

[3] Alpha is a shareware text editor for the Macintosh. More information and downloads are available from The Alpha Home page.

[4] BBEdit is a commercial text editor for the Macintosh, created by Bare Bones Software, Inc.. A demo version is available. BBEdit Lite, a freeware version of BBEdit with a reduced feature set, is also available. A comparsion, comparison page is available at the Bare Bones Software web site.

[5] Terry Morse Myrmidon and Terry Morse GoClick are "making one-click Web publishing a reality". GoClick is the professional version of Myrmidon. More information and demo versions of both products are available from Terry Morse Software,

MacPerl, currently version 5.2.0r4, is available from the CPAN in the directory /ports/mac.

MacPerl: Power and Ease (Vicki Brown and Chris Nandor, 1998, ISBN 1-881957-32-2) is published by Prime Time Freeware. MacPerl: Power and Ease is the only book to specifically cover MacPerl.

The MacPerl Pages http://www.macperl.com sponsored by Prime Time Freeware, bring you pointers to MacPerl, the mailing lists, support, tutorials, and more.