Learning How to Program in Perl

Dr. David J. Ritchie, Mr. Jim Skrine, Mr. Bruce Webber

Washington Junior High Computer Club

April 8, 1998

Summary

This is the first of several handouts for a short course in learning how to program in a computer language called "perl".

1.0 Introduction

This is the first of several handouts for a short course in learning how to program in a computer language called "perl".

2.0 What is Perl?

PERL stands for "Practical Extraction and Report Language. Some people say it stands for "Pathologically Eclectic Rubbish Lister." You are free to choose. If you choose the second one, please stop by and tell me what it means!

Often times, you will see PERL referenced as "perl" or "Perl". It is all the same.

Because perl comes from a unix heritage where upper and lower case are significant and because programs such as perl are typically referenced in all lower case, you will find "perl" to be the more common way of naming the language.

2.1 For Which Types of Computers?

Perl works for many different types of computers: unix-based computers, Windows-based computers, Macintosh-based computers, etc.

We will use perl on a Macintosh. You could also use it on a PC. In fact, the language and the programs are fairly "portable" in that you can take something written on a Mac and run it on a PC with few changes.

2.2 How much?

Perl is "free-ware". It was written originally by Larry Wall and made available to the net. It has been much enhanced and extended by the suggestions of people like you and me.

3.0 Getting Perl Going on the Macintosh

Perl has been installed on the LRC Macintosh's by Dr. David Ritchie and Mr. Skrine. You should find an icon named "perl". It will look like a camel.

3.1 To Begin

  1. Double click on the perl icon. You will launch the perl compiler. The perl compiler is the program that reads the perl script that you will write and performs the script commands.
  2. When the program launches, nothing much will change on the screen. Look in the upper right hand corner at the multi-finder place. You should see a camel. If you don't, raise your hand and I will come around and see what the problem is.
  3. Explore the Menu comands for the perl compiler. See if you can figure out what they do. We will talk about each one during our sessions.

3.2 To Continue

I will coach you on writing your first perl script. Please pay attention and follow my instructions very carefully.

4.0 The Rules of the Language

Every language has rules. English has rules. Spanish has rules. Perl has rules.

The purpose of the rules are to make it easier for the writer to communicate with the reader. If you both use the same set of rules, then it becomes easier to understand the message.

In the case of perl, you as the person programming the computer are the writer. The computer is the reader.

Computers are very picky readers. If you fail to follow the rules, the computer will complain. When it does, it usually says something like "Synatx Error on Line 10" or something like that.

After you become more experienced with perl and writing scripts that computers can understand, you will begin to appreciate how easy it is to communicate with people--in some ways, even communicating with your brother, sister, or parents are likely to be far easier than communicating with the computer.

4.1 Sentences

Ah, you thought you'd get out of DOL (Daily Oral Language) just by coming to Computer Club! Wrong!

Perl scripts are composed of sentences. You know from Language Arts that a sentence has a subject, a verb, and, usually, a direct object. In addition, it ends with some sort of ending punctuation.

That's it. Now you know how to write sentences in the language so go to it! All you need to know is the allowed verbs and the way to specify direct objects! Tell me, therefore, what does the following perl sentence ask the computer to do?

print ("Hello, world");

4.2 Non-sentences

Well, there is a little more to it. The most important non-sentence (in that it is not something whose subject is "You computer") is a comment. You can think of the subject for the comment as "You reader"--meaning the person reading the script. Sometimes, the comment is also read by the ocmputer as it is getting the perl script going.

A comment is anything from a "pound sign" ("#") to the end of the line. It is ignored by the perl compiler (most of the time). The first line of the script is the situation where it is not ignored (at least on unix boxes and sometimes on Mac boxes). A first line that reads:

#!/usr/bin/perl -w

tells the computer that the perl compiler may be found in a file named "perl" in the directory /usr/bin. The -w says that the interpreter is to issue warning messages in the case of suspicious or confusing syntax and othe rmatters.

On the Macintosh, this may or may not make any difference.

5.0 What's Next?

Along with this document, you will find some examples of simple programs in perl. Major parts of them should work for the Macintosh. Some parts will not. Your next task is to enter these programs as a sequence of scripts, to run them, and to understand what they do, and what you can do to make them work.

Next week, we will learn about the following:

  1. Scalar Data
  2. Numbers
  3. Strings
  4. Scalar Operators
  5. String Operators
  6. Scalar Variables
  7. Input
  8. Output

Appendix A

The Session Project

A.0 The Session Project

Our goal for the session is to do a project using perl of writing a perl script that will play the "hangman" spelling game with the person running the script. There are two parts--analyzing how people play hangman and then teaching the computer how to do the same thing.

A.1 Analyzing How People Play Hangman

You can begin this part immediately. For the next session, write down as a sequence of setps a recipe for how you and your friend play the spelling game "hangman."

A.2 Teaching the Computer to Play Hangman

Over the next several sessions, we will be trying to learn enough perl to be able to teach the computer to play "hangman" in the same way.

Appendix B

References

B.0 References

Here are various references to perl documents. On one hand, don't buy any of these books because you are attending these sessions. On the other hand, if you are really interested in perl, then these are some of the places to look for further information. Be sure you get your parents' and/or teachers' permission before you investigate any of the web sites.

B.1 Books, etc.

You can get books on perl at Anderson's Bookstore and at Books and Bytes. You can also get books on perl at Borders and Barnes and Nobel.

B.2 Web Pages, etc.

At least one web page is the following.  There are many others as you will find if you fire up your favorite search engine and look for "perl".