1.1 The First Perl Program
Program 1.1
#!/usr/bin/perl #file hello.pl print "Hello, world! \n";Technically, this program has only one line or statement of code. The first two lines are comments. # starts a comment in Perl. When Perl finds # on a line of input, it ignores the line starting at the next character till the end of the line.
The first line is needed only on a Unix system to indicate where the Perl interpreter is situated in the system. Although, it is not necessary for a comment line to start in the first column, this particular first line must start in the first column. In the particular Unix system where we run the program, the Perl interpreter is /usr/bin/perl. The #! is used as a convention in many interpreted languages to tell the underlying Unix system where to find the interpreter. If you are using a Unix system and the Perl interpreter is located somewhere else, you will have to find out the full name from your systems administrator and put it in place of the pathname given here.
On non-Unix systems, the first line of comment is unnecessary. However, having the first line of comment doesn’t hurt on non-Unix systems. In fact, it is a good idea, especially if you want your program to be portable across platforms. The second line is also a comment. Of course, this line is not required on any system. This comment line does not have to start in the first column as shown in the sample code above. It says that the program is stored in the file hello.pl, and is useful for a human reader.
The only real line of code in this program is the print statement. The print command, as used here, takes a string as its only argument. The string is started and ended, i.e., delimited by double quotes. The last character in the string is \n. If used inside a string enclosed in double quotes, as we do here, \n, literally a sequence of two separate characters, means the newline character. In other words, it ends a line of string and starts a new one. Such a two-sequence character with \ in the front is considered a single unit and is called a backslashed escape sequence or simply an escape character. Perl allows strings to be enclosed within single quotes also. If a string is single quoted, a backslashed escape character like \n, stands for two individual characters and does not make special sense as a two-character sequence with \ in the front. A statement is terminated by ; in Perl.
Let us assume that the program is stored, as stated in the comment line, in the file hello.pl. Note that Perl doesn’t require program files to have any specific extension at the end of the file’s name although we have used the .pl. extension here. On a Windows system, the extension .pl can be associated with the Perl interpreter. We can create the file in any editor with which we are comfortable.
On any system, whether Unix or non-Unix, we can run the program in a terminal by giving the file name as an argument to the perl command as given below.
perl hello.plHere, we assume that % is the system prompt. There is no need to compile the program since Perl is an interpreted language.
On a Windows system, one can also run the Perl program by by making a choice on a pop-up menu that says something like Execute Perl script and then select the name of the file containing the code to run. To run the program on a Unix machine, one can also do the following. First, we need to make the file executable, i.e., give it execute permission for the user. Then, we run the program by typing its name at the system prompt. On a terminal, we type the following.
%chmod u+x hello.pl %hello.plNote that changing the file’s permission to allow execution needs to be done only once, even if we edit the program file later. In a Unix system, the chmod command changes the modes or permissions associated with files. The first argument u+x asks the system to add the execute permission for the user. u stands for user, and x stands for execute permission. The plus sign (+) adds the permission. The last argument to the chmod command is the file name hello. We are assuming that % is the Unix prompt. We don’t have to type the % sign.
The output of the Perl program is simply
Hello, world!as expected.
