3

Chapter 0:

Introduction

Humans like history,
like to know why things start and end,
like to have reasons for it...

- James Herndon, How to Survive in Your Native Land

For many people, programming is a fearsome and formidable thing. Pro-
grammers are the wizards of this modern technological world, considered
by many to have powers beyond those of ordinary "users". The Macintosh,
with its user-friendly interface, ease of use, and dearth of accessible pro-
gramming tools, has, we're sorry to say, perpetuated this belief.

On many computer systems, programming languages are just an extension of
the normal command languagethat makes the computer work. The Macin-
tosh's mouse-oriented interface, although comfortable to use, insulates its
users from both command and programming languages.

The purpose of this book is to show you that programming is within your
grasp. Like the rider on our cover, you can harness the power of the pro-
gramming dragon. Our Perlescent dragon is formidable, and extremely
powerful, but the rider has him firmly in hand. Together, they can work
wonders of great power, going places they never dreamed possible, all
through the "magic" of programming.

What Is Perl?

Perl(and, by extension, MacPerl) is a convenient and powerful language for
administrative programming, CGI scripting on the World Wide Web, data


IMAGE imgs/105.Intro01.gif

analysis and filtering (e.g., error checking and reformatting), network pro-
gramming, and more. In short, Perl can be used for almost any programming
project you may have in mind.

Note:Perl is either the "Practical Extraction and Report Language" or,
less formally, the "Pathologically Eclectic Rubbish Lister". Really.

Perl is an interpreted1language with a substantial support library. Because
most Perl code is distributed in source (human-readable) form and because
the library code has been portedto many operating systems, Perl code can be
quite portable. Whether a piece of code actually isportable depends, as
always, on the programming techniques chosen by the code's author(s).

On Unix systems, Perl is well on its way toward taking over all substantial
scripting functions, supplanting traditional Unix tools such as
sh("Bourne
shell"),
awk, and sed. On Macintosh systems, Perl can be used just as read-
ily (with the added attraction that there is nothing to "unlearn").

Perl was created in 1987 when its author, Larry Wall, decided that existing
scripting languages were insufficiently powerful for the distributed bug
reporting project he was working on. Seeing the potential in his new tool,
Larry was gracious enough to release Perl as freeware(freely redistribut-
able software).

Other programmers picked it up, tried it, liked what they saw, and sugges-
ted enhancements and modifications. In only a few years, Perl grew substan-
tially, both in capabilities and in adherents, and was soon well on its way
to becoming one of the most powerful and popular computer languages in use
today. Although Perl was originally written for the Unix operating system,
it has since been ported to (made to work with) many different systems.

The recent rise in popularity of the World Wide Web has assured the popu-
larity of Perl for some time to come. Perl is not a "strongly-hyped language"
like Java, but it has shown itself to be an indispensible tool for creating and
maintaining Web sites. Perl is used for CGI scripting, site management, and
many other duties.

IMAGE imgs/105.Intro02.gif

1To be precise, Perl code is not interpreted directly. The code is first "compiled" into a
more executable form (i.e., syntax trees) within the Perl process. This modified form of
the code is then interpreted. A "real" compiler (i.e., one which generates machine codes)
is also under development, for those who care strongly about either execution speed or
the ability to distribute binary (hence, relatively private) programs. (We don't!)


IMAGE imgs/105.Intro03.gif

MacPerl has also been in existence for several years (the first version was
released in 1991), but its popularity has not increased at the same rate, and
certainly not to the level we feel it deserves. The Macintosh is a friendly,
easy to use, and very popular computer system. MacPerl is an elegant and
friendly Macintosh adaptation of an extraordinarily powerful (and popu-
lar) programming language. Why, we wonder, hasn't it taken off?

We believe that the lack of clear, printed documentation and formal distri-
bution methods has been a large part of the problem. With no introductory
or printed reference material, and no Macintosh-oriented distribution CD-
ROM, many prospective MacPerl users might well feel apprehensive about
getting involved.

And, because very few Mac-related magazines cover programming issues,
many potential MacPerl users may not even know that a Macintosh version
of Perl exists! It is our hope that this product will resolve most of these
issues, helping the MacPerl community to grow substantially.

What About AppleScript?

The Macintosh is a wonderful, easy-to-use, and powerful system. It is also
inherently interactive. To perform a task, the user must (usually) move the
mouse, select an icon, click a button, and otherwise interact with the compu-
ter. Sometimes, it would be nice to automate these interactions so they can
take place without the user's presence, intervention, and possible mistakes!

AppleScript is Apple's answer to this need. AppleScript is a scriptinglan-
guage for the Macintosh. The scriptssend Apple Events (request and notifi-
cation messages) to scriptable applications(applications that have been
designed to accept these events). Using AppleScript, any Macintosh user
can write or run a simple program to automate many of the ordinarily inter-
active aspects of using the Mac.

For example, one could use a script to tell the Finder to open an application
or to empty the Trash. AppleScript can tell applications to do things as
well; you might use a script to tell your email program to connect to the
mail server and download any new messages. Thus, if you have used Apple-
Script, you may be asking yourself what MacPerl can do for you that
AppleScript cannot. The answer is: plenty!


IMAGE imgs/105.Intro04.gif

AppleScript was designed to "talk to" Macintosh applications, automating
steps that users would otherwise perform interactively (using the mouse
and menus). Perl, in contrast, was designed to work primarily with data.

A typical Macintosh system holds a large number of data files: spread-
sheet and data base files, word processor documents, and more. In fact, to
the Finder, every document (and folder!) is a form of data. Where Apple-
Script excels at working with programs, Perl excels at working with data -
performing extractions, calculations, or transformations at your command.

Using MacPerl, you can have the best of both worlds. MacPerl is able to
send AppleScript commands and Apple Events from inside of your MacPerl
program. Thus, you can use the interaction automation features of Apple-
Script in conjunction with the powerful data transformation features of
Perl, all in one powerful program. This feature of MacPerl is so important
that we've devoted an entire chapter (in Part III of this book) to Apple-
Script and Apple Event programming with MacPerl.

What About Other Languages?

Although the Macintosh was designed to be usable without requiring its
users to program, programming languages for the Macintosh have been in
existence and readily available for many years. These have ranged in com-
plexity and cost from simple and inexpensive (e.g., HyperCard) to complex
and often expensive (e.g., commercial C/C++code development packages).

With the advent of Apple Events, several new languages have become
available which comply with Apple's Open Scripting Architecture(OSA).
We covered AppleScript in the previous section. UserlandFrontieris a very
popular freeware OSA compliant language with a more algebraic syntax.

Many application programs, such as spreadsheets and data bases, contain
specialized internal scripting languages (e.g., Visual Basic). Freeware and
shareware implementations of common languages, such as Tcland Python,
are available for downloading from many servers. Why, then, should you
be learning MacPerl?

First, it's not just because we think Perl is better than all of these other lan-
guages.
2Perl is a very popular and well-used language. When you start

IMAGE imgs/105.Intro02.gif

2We actually do think so, but we'd probably get into an argument with some people if
we said so, and that's not what we're here for!


IMAGE imgs/105.Intro06.gif

learning Perl, you'll be able to find many books on the subject (including this
one!). You'll also be able to find many people (e.g., on the USENET) who
can help answer your questions if you get stuck.

Fundamentally, however, most popular computer languages are good for
something; if they weren't, they would no longer be popular. When choos-
ing a language for a project, you should use a combination of criteria:

  • Are you already competent in a language that can do the job?
  • What special features would you like (i.e., help to make a language
    particularly well suited to the problem you need to solve)?
  • Are there books, tools, user groups, and libraries of examples and sup-
    port code to help you get started in using the language?
  • Will the language you choose be applicable to other problems in the
    future? That is, can you leverage your programming skills?

Perl is an excellent, flexible language that can be used for many different
types of projects. Perl's large and active user community has provided many
examples, resources, and tools. We think you'll like Perl, once you've tried
it. Rather obviously, that's why we wrote this book!

Even if you've already been programming on the Macintosh in another lan-
guage, give MacPerl a try. You don't have to give up on other languages, but
you are quite likely to be very impressed by the things MacPerl can do.

If, after trying MacPerl, you don't find that it fits your needs, feel free to
give this book to a friend. The choice of language is also a personal one,
after all the technical reasons have been satisfied. The most important
criterion is that you be able to solve the problems you encounter with the
language you choose.

Scope

This book has a very large charter. It starts with introductory computer
programming concepts, moves on to actual syntax (using MacPerl), then pro-
ceeds to increasingly complex and specific topics, including binary file man-
ipulation, CGI scripts, network programming, and more. In order to meet
these goals, we have made some carefully-considered compromises.

Specifically, this book omits or abbreviates coverage of some advanced Perl
constructs. These are well covered by other books (in particular, the O'Reil-


IMAGE imgs/105.Intro07.gif

ly offerings) and would have made this book unreasonably large. Because in
Perl "There's More Than One Way To Do It" (TMTOWTDI), we have been
able to demonstrate all of the critical MacPerl concepts without resorting to
the more esoteric Perl programming idioms.

You will be able to make very good use of MacPerl without venturing into
Perl's more arcane constructs. Consequently, this book can function as a self-
contained reference for the essential parts of the language.

We recommend, however, that you go on to investigate the larger world of
Perl programming. Perl is a very big and powerful language; a proper under-
standing of it will introduce you to a substantial amount of computer science!

Audiences

Wherever there are beginners and experts, old and young,
there is some kind of learning going on,
and some kind of teaching.
We are all pupils and we are all teachers.

Gilbert Highet, The Art of Teaching, 1950

The prospective audience for this book, like the Macintosh community, is
extremely diverse. Some readers will be experienced programmers; others
will have no knowledge of programming at all. In addition, the readers'
motivations will vary, spanning curiosity, business or educational needs,
and more; no one description could possibly cover every possible reader!

Some things, however, are clear. Although you do not need to have any
particular knowledge about programming, you should be comfortable with
Mac OS and interested in learning how to make better use of your machine.

As an experienced Macintosh user, you should know how to launch applica-
tions, use menus, edit text, and so forth. In addition, you should recognize
and be familiar with the most common menus -
File, Edit, Help- and their
standard items -
New, Open, Close, Save, Quit, Copy, Cut, Paste, Undo.

You should also have an interest in learning new things. Specifically, you
should be interested in learning how to program your Macintosh! Although
we are willing to take on students who merely needto learn programming,
our real motivation comes from those who really wantto learn about it!


IMAGE imgs/105.Intro08.gif

It is not necessary, however, that you already know how to program com-
puters. Some programming knowledge (in whatever language) is certainly a
plus, but this book can teach you programming from scratch, if need be. At
some point, you will want to peruse the generic Perl reference material, but
the book provides enough background material to make that leap possible.

This book also addresses the needs of experienced programmers who wish to
learn about MacPerl in particular. It covers many issues that are specific to
the Mac environment: Apple Events and AppleScript, droplets, line termi-
nation, MacPerl runtimes, MPW, resource forks, text editors, and more.

Here are some profiles of prospective MacPerl users, listed in alphabetical
order.
3See if you recognize yourself (or a friend) ...

Bill uses his Mac strictly as a personal computer. Out of curiosity, Bill has
tried programming in AppleScript and HyperTalk, but found them rather
verbose and not very powerful. Bill would like to learn a language that can
help him to learn more about programming.

Don has spent the last twenty years working as a landcape architect, using
computers only rarely. He is considering starting a new career and sees Mac-
Perl as a way to learn something about computers and programming before
he invests time and money on attending courses at the local university.

Doug uses his Mac to design specialized electronic circuitry and supporting
software. He programs MacPerl on a regular basis, using MPW (Macintosh
Programmer's Workshop). Doug would love to have a MacPerl reference,
particularly if it covers esoteric areas like Apple Event programming.

Ed has used personal computers for more than a decade, but now uses a Mac-
intosh for all of his work. Ed has done some programming in BASIC and
Pascal, but has never been motivated to learn C. MacPerl appeals to him,
however, because of its powerful language features and high degree of
Macintosh integration.

Erik has been doing network programming for years. Finding himself tied to
a proprietary mail system, he created an Internet mail gateway using Perl.

IMAGE imgs/105.Intro02.gif

3To make things easier for the authors, each of our profiles is taken from real life. The
names have not been changed. We apologize if our sketches do not meet the current
norms of "political correctness" but real people are rarely perfect, so there you are.


IMAGE imgs/105.Intro10.gif

Erik reads manual pages for relaxation, so he doesn't need any tutorial
material, but he thinks that a MacPerl reference would be nice to have
around. (He also likes the idea of having a Mac OS-readable CD-ROM
that contains copies of MacPerl, the CPAN, and other useful material!)

Joyce is a technical writer and editor who needs to maintain documents in
various formats. She wants to ensure that each document matches the latest
formatting standards, even after those standards change. Perl can be used
for file checking: "Does this file match a specified format?" Perl is also
adept at reading quasi-text formats, such as tab-separated exchange files.

Marilyn, a graphic designer, occasionally finds herself creating, designing,
and/or specifying Web pages. She finds that Perl is used everywhere in the
Web, not only for CGI scripts, but also (in Apache) for server administration
and extensions. So, she needs to learn about programming in general and CGI
(Common Gateway Interface) scripting in particular.

Rick has been programming the Macintosh for years, primarily in Assembly
language, C, and Pascal. He also works with Unix systems, however, and
finds that he misses the Unix scripting capabilities when he works on the
Mac. Rick is interested in learning MacPerl as a fast prototyping tool.

Shira, the comptroller of a small high-tech company, uses computers in her
work, but has never had any interest in learning how to program them. She
has found, however, that the spreadsheets and other tools she uses are not
able to perform the diverse kinds of analysis she requires. In addition, she
needs a way to move data in and out of these tools, even when the formats
are incompatible.

Tom, a retired professor, uses his Macintosh mostly to play games and send
email. He's interested in learning more about his computer, however. Speci-
fically, he'd like to understand a little more about programming and what
makes "programs" different from other types of files.

Valerie has been using a Mac for several years, but has never learned how
to program it. She is about to enter secondary school, however, and will be
needing to do calculations for homework assignments, etc. She thinks that
MacPerl will allow her to learn about programming while staying within
her well-loved Macintosh environment.


IMAGE imgs/105.Intro11.gif

Roadmap

Most programming books assume that the reader knows how to program in
some other language. This eases the task for the authors, but it doesn't help
non-programmers to get started. We want all sorts of users to get involved
with MacPerl, so we have taken the effort to start with the basics.

If you are totally new to programming, we recommend that you begin with
Chapter 1, A Peek Into Programming. It gives an overview of computer pro-
gramming concepts and terminology, without resorting to language-specific
constructs or syntax. Chapter 2, A Dip Into Data, then explains key concepts
and terminology regarding data representation, structure, etc.

If you are an experienced programmer, but want an informal introduction to
Perl syntax and concepts, we recommend that you begin with Chapter 3, A
Step Into Syntax
. Perl syntax is very eclectic and a bit unusual in spots. This
chapter lets you take "a step into the water" before you jump into the pool.

All readers should work their way through Chapter 4, Installation And
Overview
. Not only do the following chapters assume that you have a
running copy of MacPerl on hand, they will assume that you are familar
with some of the user interface features (dialogs, menus, windows, etc.)
covered in this chapter. Feel free to skim, but do read this chapter!

If you are comfortable with computer programming, but are unfamiliar with
Perl, we suggest that you proceed to Part II, Learning The Language. Using a
series of paced examples, this part introduces Perl concepts and syntax. In
particular, it highlights some of the things that make MacPerl different
from, say, Unix Perl.

Part III, Advanced Topics, will tell you how to do all sorts of useful and
interesting things with MacPerl. Finally, Part IV, Reference, will serve as
a place to look up some of the Perl constructs you will encounter.

Experienced MacPerl programmers may wish to skip much of Parts I and II,
moving on to Part III, Advanced Topics. You know who you are; read the
table of contents and have fun. If you get lost, Parts I and II are quite willing
to take you in, soothe your pains, and get you started again ...

You should also consider investing in some of O'Reilly and Associates' Perl
(and related) books at this point. O'Reilly is the definitive leader in Perl


IMAGE imgs/105.Intro12.gif

publishing; take advantage of their fine efforts! Here are some favorites,
along with a rough graphical indication of their level of difficulty:

MacPerl: Power and Ease* * * * * *
Learning Perl* * * * *
Programming Perl* * * * *
Web Client Programming with Perl* * * *
Perl Resource Kit* * * *
Mastering Regular Expressions* * * * *
Advanced Perl Programming* * * *

Advice

Perl is a very eclectic language, so you may find things that are familiar to
you. The influence of the C programming language is obvious, but Perl also
draws upon constructs from Unixish programming tools (
awk, sed, sh, tr,
etc.). Be cautious, however, in assuming that a character sequence that
looks familiar will act precisely as you expect: Perl is an integrated
language, not a grab-bag of syntax.

Because TMTOWTDI, Perl code from other authors may contain constructs,
idioms, or even syntax that we do not cover. If you are using code from other
authors (not an unlikely event!), you will need to learn a broader range of
Perl than we present.

Fortunately, O'Reilly and Associates (www.oreilly.com) has published
some excellent books on Perl and related topics. Start with Learning Perl,
moving on to Programming Perl(the "camel book")and Advanced Perl Pro-
gramming
. These books, and many others, are listed in Part V, Resources.
For detailed information on related topics, visit O'Reilly's Web site.

More generally, you should take a look at Part V of this book (Resources).
We have brought together a large list of books, CD-ROMs, email lists, web
sites, and other useful sources of information. Do yourself a favor by becom-
ing familiar with what's available!

Why Chapter 0?

This is a book about (Mac-)Perl, which considers 0 to be a "first class citi-
zen" (Perl arrays, for instance, start with position 0). In order to learn Perl,
you may have to change some of your ways of thinking. So, consider this to
be your initial introduction to "thinking in Perl".

Copyright © 1997-1998 by Prime Time Freeware. All Rights Reserved.