LINFO

How to Create a First Perl Script



Perl (short for Practical Extraction and Report Language) is one of the most popular programming languages. It was developed by Larry Wall, who was trained as a linguist, mainly for processing text, but it is now also used for a much broader range of applications, particularly systems administration and web development.

In addition to its outstanding text processing capability, Perl's features also include ease of use, power and portability (i.e., the ability to be used on virtually any operating system). Moreover, it is free software; that is, it is free both in a monetary sense and with regard to use.

Perl is usually installed by default at the same time that the operating system is installed on computers running Linux and other Unix-like operating systems. An easy way to see if it has actually been installed on any machine is to type in the following command and then press the ENTER key:

perl -v

perl (beginning with a lower case p) is the Perl command. The -v option causes it to tell the version that is installed.


First Program

The usual starting point in becoming familiar with Perl is to write and execute a very simple script such as the following, which writes the phrase Hello to the world! on the display screen. A script is a simple program.

All that is necessary to create this script is to create a plain text file with a text editor, such as gedit or vi, that contains the following two lines of code and save it with a name that has a .pl extension, such as perl1.pl:

#!/usr/local/bin/perl
print "Hello to the world!\n";

The first line is generally not necessary on Unix-like operating systems for simple Perl scripts, but it is useful to become familiar with it and get in the habit of using it. This line informs the system that the file is a Perl script and tells it the location of the Perl executable and associated files. It is referred to as the shebang line because the pound sign followed by an exclamation mark is called a shebang. Shebangs are used in other scripting languages as well.

print is one of the most commonly used functions in Perl. It tells Perl to display what follows it in quotes on the display monitor or to write it to a file. The \n represents the newline character; it tells Perl to begin a new, blank line on the screen following the text. Note that this line ends in a semicolon, as is the case for statements (i.e., individual instructions) in programs.

An alternative way to create this file is with the cat command (which is used to read, create and combine files) by first typing the following

cat > perl1.pl

then pressing the ENTER key, next typing in print "Hello to the world!\n"; on the new line that appears, then pressing the ENTER key and, finally, pressing the CONTROL and c keys simultaneously.

On a typical system this file will, by default, not have its permissions set as executable (i.e., ready to run as a program). However, this can easily be changed by using the chmod command (which is used to change permissions). Assuming that the file is named perl1.pl and that it was saved in the current directory, this would be accomplished with the following:

chmod 755 perl1.pl

The program is now ready to run. It can be run by merely issuing the perl command and using the name of the new file as an argument (i.e., input data) as follows:

perl perl1.pl

If all goes well, the text Hello to the world! should appear on the display screen.


Troubleshooting

If nothing appears on the screen or if there is an error message, the first step is to confirm that Perl has actually been installed on the system, as shown above. If it has, a very likely cause of the problem is that the .pl extension was omitted either when saving the program or when attempting to run it with the perl command. Another possibility is that there is some error in the spelling of the file name when using that command.

If none of these are the cause of the problem, it is likely that the problem lies with the content of the file. Thus, the next step should be to open the file with a text editor to confirm that it actually contains the text that was intended to be written to it. The text could be missing, particular if the cat command was used incorrectly. Also, it should be confirmed that the text is written exactly as shown in the example above, particularly that the semicolon is included at the end of the line. Forgetting to put the semicolon at the end of a statement is one of the most common programming errors.


Experiments

Although the above-discussed script is extremely simple, it provides a good basis for learning about more of Perl's capabilities and creating more useful programs. Before going on to more advanced programs, however, it can be instructive to conduct several simple experiments with this program.

One of them is to make a change in the text and then rerun the script. An example is to replace it with something like This is my first Perl script. The changes could also include omitting the newline character or using two newline characters, with the resulting program looking like print "This is my first Perl script!\n\n";, or adding some text after the newline character(s). These changes can be made to the original file, or a new file (with a new name) can be created.

Another experiment is to try omitting the semicolon at the end of the statement and observe the error message that will appear. An extremely simple program such as this may work, but it is not good programming practice and more complex programs will generate an error message.

Experiments can also be conducted with the quotation marks. For example, it can be seen that removing one or both sets of double quotation marks will result in an error message such as Can't find string terminator '"' anywhere before EOF at aaa2.pl line 1.. It can be observed that replacing the two sets of double quotes with two single quotes will change how perl interprets the program: it will cause the \n to just be interpreted as text rather than as an instruction to begin a new line.






Created October 17, 2006.
Copyright © 2006 The Linux Information Project. All Rights Reserved.