Networking | Programming | Security | Linux | Computer Science | About

An introduction to Perl

When it comes to writing scripts, the Perl language is very popular among system administrators, especially on UNIX and Linux systems. System administrators use Perl to automate routine system administration tasks such as looking for old files that could be archived and deleted to free up disk space. Perl is a scripting language, which means that you do not have to compile and link a Perl script (a text file containing Perl commands). Instead, an interpreter executes the Perl script. This capability makes writing and testing Perl scripts easy because you do not have to go through the typical edit-compile-link cycles to write Perl programs. Besides ease of programming, another reason for Perl’s popularity is that Perl is distributed freely and is available for a wide variety of operating systems, including Linux and many others such as UNIX, Windows 95/98/NT/2000/XP, and Apple Mac OS X.

What is Perl?

Officially, Perl stands for Practical Extraction Report Language, but Larry Wall, the creator of Perl, says people often refer to Perl as Pathologically Eclectic Rubbish Lister. As these names suggest, Perl was originally designed to extract information from text files and to generate reports. Perl began life in 1986 as a system administration tool created by Larry Wall. Over time, Perl grew by accretion of many new features and functions. The latest version — Perl 5.8 — supports object-oriented programming and allows anyone to extend Perl by adding new modules in a specified format.

True to its origin as a system administration tool, Perl has been popular with UNIX system administrators for many years. More recently, when the World Wide Web (or Web for short) became popular and the need for Common Gateway Interface (CGI) programs arose, Perl became the natural choice for those already familiar with the language. The recent surge in Perl’s popularity is primarily due to the use of Perl in writing CGI programs for the Web. Of course, as people pay more attention to Perl, they discover that Perl is useful for much more than CGI programming. That, in turn, has made Perl even more popular among users.

Perl is available on a wide variety of computer systems because, like the Linux operating system, Perl can be distributed freely.

If you are familiar with shell programming or the C programming language, you can pick up Perl quickly. If you have never programmed, becoming proficient in Perl may take a while. I encourage you to start with a small subset of Perl’s features and to ignore anything you don’t immediately understand. Then, slowly add Perl features to your repertoire.

Do you have Perl installed?

Before you proceed with the Perl tutorial, check whether you have the perl program installed on your Linux system.

Type the following command:

which perl

The which command tells you whether it finds a specified program in the directories listed in the PATH environment variable.

If perl is installed, you see the following output:


You should find Perl already installed in all Linux distributions. Another way to check for Perl is to type the following command to see its version number:

perl -v

Here is typical output from that command:

This is perl, v5.8.7 built for i586-linux-thread-multi Copyright 1987-2005, 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 source kit.

Complete documentation for Perl, including FAQ lists, should be found on this system using `man perl’ or `perldoc perl’. If you have access to the Internet, point your browser at, the Perl Home Page.

This output tells you that you have Perl Version 5.8, patch level 7, and that Larry Wall, the originator of Perl, holds the copyright. (Remember, however, that Perl is distributed freely under the GNU General Public License.) You can get the latest version of Perl by pointing your World Wide Web browser to the Comprehensive Perl Archive Network (CPAN). The following address connects you to the CPAN site nearest to you:

Writing Your First Perl Script

Perl has many features of C, and as you may know, most books on C start with an example program that displays Hello, World! on your terminal. Because Perl is an interpreted language, as opposed to C and C++, which require compiling before the program can be run, you can accomplish this task directly from the command line. If you enter

perl -e ‘print “Hello, World!\n”;’

Perl responds with the following:

Hello, World!

This command uses the -e option of the perl program to pass the Perl program as a command-line argument to the Perl interpreter. In this case, the following line constitutes the Perl program:

print “Hello, World!\n”;

To convert this line to a Perl script, simply place the line in a file and start the file with a directive to run the perl program (as you do in shell scripts, when you place a line such as #!/bin/sh to run the shell to process the script).

To try this Perl script, follow these steps:

  1. Use a text editor to type and save the following lines as a file named

     #!/usr/bin/perl # This is a comment. print “Hello, World!\n”;

  2. Make the file executable by using the following command:

    chmod +x

  3. Run the Perl script by typing the following at the shell prompt:


It displays the following output:

Hello, World!

That’s it! You have written and tried your first Perl script. Notice that the first line of a Perl script starts with #!, followed by the full pathname of the perl program. If the first line of a script starts with #!, the shell simply strips off the #!, appends the script file’s name to the end, and runs the script. Thus, if the script file is named and the first line is #!/usr/bin/perl, the shell executes the following command:


Published on Sun 17 June 2007 by Adrian Foster in Programming with tag(s): perl