Introduction to Perl


Fisrt part covers the basic concepts of the Perl language. In the second part, you move on to advanced concepts-how to create Perl statements and whole programs. Last part shows you how to explore more advanced programming topics.

The next section on Perl's origin is very short. You can read all the background information at http://www.perl.com-the Perl home page.

Perl Origins

Originally Perl meant the Practical Extraction Report Language.

Back in 1986, Larry Wall invented the Perl language as an interpreter on UNIX. The new language had an emphasis on system management and text handling. After a few revisions, it could handle regular expressions, signals, and network sockets too.

Similar to C?

Perl programs bear a passing resemblance to C programs, perhaps because Perl was written in C, or perhaps because Larry found some of its syntactic conventions handy. But Perl is less pedantic and a lot more concise than C.

Perl can handle low-level tasks quite well, particularly since Perl 5 has a lot in common with C. But Perl handles the internals of data types, memory allocation and such automatically and seamlessly.

This habit of picking up interesting features as it went along-regular expressions here, database handling there-has been regularized in Perl 5. It is now fairly easy to add your favorite bag of tricks to Perl by using modules. It is likely that many of the added-on features of Perl such as socket handling will be dropped from the core of Perl and moved out to modules after a time.

Perl is free

The full source code and documentation are free to copy, compile, print, and give away. Any programs you write in Perl are yours to do with as you please; there are no royalties to pay and no restrictions on distributing them as far as Perl is concerned.

It's not completely "public domain". It is released under the terms of the "Artistic" license. This is a variation on the GNU General Public License which says that anyone who releases a package derived from Perl must make it clear that the package is not actually Perl. All modifications must be clearly flagged, executables renamed if necessary, and the original modules distributed along with the modified versions. The effect is that the original author is clearly recognized as the "owner" of the package. The general terms of the GNU General Public license also apply.

Installation of Perl on your computer

It's critically important to have Perl installed on your computer before reading too much further.

It is very easy to see if your system already has Perl installed. Simply go to a command-line prompt and type:

perl -v

The response will be similar to this:

This is perl, version 5.004_04 built for irix-n32

Copyright 1987-1997, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5.0 source kit.

If you get an error message or you have version 4 of Perl, please see your system administrator or install Perl yourself. The next section describes how to get and install Perl.

Getting and Installing Perl

New versions of Perl are released on the Internet and distributed to Web sites and ftp archives across the world. UNIX binaries are generally not made available on the Internet, as it is generally better to build Perl on your system so that you can be certain it will work. All UNIX systems have a C compiler, after all.

Each operating system has its own way of getting and installing Perl.

For UNIX and OS/2, the Perl Home Page contains a software link (http://www.perl.com/perl/info/software.html) that will enable you to download the latest Perl source code. The page also explains why Perl binaries are not available. Hopefully, your system will already have Perl installed. If not, try to get your system administrator to install it.

For Windows 95/98/2000/Windows NT, the home page of hip communications, inc. (http://www.perl.hip.com) contains a link to download the i86 Release Binary. This link lets you download a zip file that contains the Perl files in compressed form.

Instructions for compiling Perl or for installing on each operating system are included with the distribution files. Follow the instructions provided and you should having a working Perl installation rather quickly.

Your First Perl Program

Your first Perl program will show how to display a line of text on your monitor. First, you create a text file to hold the Perl program. Then you run or execute the Perl program file.

Creating the Program

A Perl program consists of an ordinary text file containing a series of Perl statements. Statements are written in what looks like a amalgam of C, UNIX shell script, and English. In fact, that's pretty much what it is.

Perl code can be quite free-flowing. The broad syntactic rules governing where a statement starts and ends are:

Here's a Perl's simple statement inspired by Kurt Vonnegut:

print("Hello World!\n");

Perl runs this code and prints out Hello World!. \n simply means that Perl should print a newline character after the text, or in other words, go to the start of the next line.

Printing more text is a matter of either stringing together statements like this, or giving multiple arguments to the print() function:

print("My name is Jun Ni,\n");
print("I am an instructor and\n", "I am teaching Perl programming language.\n");

So what does a complete Perl program look like? Here's a small example, complete with the invocation line at the top and a few comments:

#!/usr/local/bin/perl -w
print("My name is Jun Ni,\n");
print("I am an instructor and\n", "I am teaching Perl programming language.\n");

That's not at all typical of a Perl program, though; it's just a linear sequence of commands with no complexity.

You can create your Perl program by starting any text processor:

Let's create a file called test.pl that contains the preceding three lines.

Invocation

Assuming that Perl is correctly installed and working on your system, the simplest way to run a Perl program is to type the following:

perl filename.pl

For example,

perl test.pl

This example assumes that perl is in the execution path; if not, you will need to supply the full path to perl too. For example, on UNIX the command might be:

/usr/local/hin/perl test.pl

Whereas on Windows, you might need to use:

c:\perl5\bin\perl test.pl

UNIX systems have another way to invoke a program. However, you need to do two things. The first is to place a line like

#!/usr/local/bin/perl
at the start of the Perl file. This tells UNIX that the rest of this script file is to be run by /usr/local/bin/perl. The second step is to make the program file itself executable by changing its mode:

chmod +x test.pl
Now you can execute the program file directly and let the program file tell the operating system what interpreter to use while running it. The new command line is simply:

test

Comments in Your Program

It is very important to place comments into your Perl programs. Comments will enable you to figure out the intent behind the mechanics of your program.

Comments are placed inside a program file using the # character. Everything after the # is ignored. For example:

# This is whole line is ignored.
print("Perl is easy.\n");      # Here's a half-line comment.

Review Questions

Answers to Review Questions are in Appendix A:

  1. What is the address of Perl's home page?

  2. Who was the creator of Perl?

  3. How much does Perl cost?

  4. Why are comments important to programming?

Review Exercises

  1. Connect to the Perl Home page and spend a few minutes looking at the links.

  2. Create and run a Perl program that prints "Hello, World" on the monitor.