-->
Previous Table of Contents Next


Chapter 28
Perl

by Tim Parker

In This Chapter
•   What is Perl?
•   Creating and executing Perl programs
•   Handling data in Perl
•   Arrays
•   Perl programming constructs
•   Functions
•   Perl operators
•   Converting programs to Perl

Perl, which stands for Practical Extraction and Report Language, is an interpreted programming language that was developed by Larry Wall. Perl was initially designed to make scanning and manipulating text files easier than it is in languages such as awk. This chapter includes the following topics:

  What Perl is
  Creating and executing Perl programs
  Data, variables, and arrays
  Perl programming constructs
  Perl functions

After reading this chapter, you should be familiar with some of the advantages that Perl offers over other programming languages. You should also be able to write simple Perl programs to help you in your day-to-day interactions with Linux.

What Is Perl?

As stated, Perl was originally created to make scanning and manipulating text files easier. People who are familiar with the awk programming language (see Chapter 25, “gawk”) will not be surprised to learn that Perl borrowed many of its features from awk. Perl also includes some of the best features of C, sed, and the UNIX shell languages such as bash and tcsh.

Perl is very similar to the shell programming languages both in syntax and in function. There are, however, a few differences worth mentioning. One of the biggest differences between how Perl works and how shell programs work is that Perl is not purely an interpreted language. Perl programs are actually read in full and stored in an intermediate form before they are executed. Shell programs are read and executed one command at a time. This offers two advantages for Perl over shell programs. First, Perl programs execute much faster than shell programs. This is because the Perl interpreter does all the syntax checking and stripping of comments before the code execution is started. Second, you don’t have to worry about a large shell program stopping halfway through its execution as a result of a syntax error because all the code is parsed before it starts executing.

The fact that Perl is not a pure interpreted language also has one disadvantage. For small programs whose execution time is not that large, the extra compile time involved in using Perl actually makes it slower than using a shell language. This is a relatively small problem and one that few people actually notice.

Creating and Executing Perl Programs

The steps you take to create and execute a Perl program are very similar to those used in creating and executing a Linux shell program. Perl programs consist of one or more Perl commands that are placed into a text file. The first thing you must do to create a Perl program is create a file in which to put Perl commands. You can do this using your text editor of choice. Let’s start with a very simple example of a Perl program. The following Perl program, called hello, prints a greeting on the screen.


#!/usr/bin/perl

print “Hi there!\n”;

There are several things worth mentioning about this example. The first is the strange-looking first line of the program. This line tells the shell program what program to run to execute the code contained in the file.

The second line of the program prints Hi there!, followed by a new line on the screen. C programmers will notice how much this line looks like C code.

You have one more step to take before the program will actually work, and that is to change the permissions on the file so it is executable. This is done by entering the following command:


chmod +x hello

You can now run the program by typing the following on the command line:


hello

Running this program causes the Perl interpreter to be invoked. The Perl interpreter parses the whole program and then executes the compiled code.

This is the standard way of running a Perl program. You can also run the hello program by invoking Perl on the command line and passing the hello code to it as a command-line parameter. This is done by entering the following command:


perl hello


Previous Table of Contents Next