Your First Perl Program

Several issues face a programmer who is about to write his or her first program in an unfamiliar programming language. How is the text for the program prepared? How do I translate this program into something that can be executed by the machine? How is it executed? These issues reside more or less outside of the programming language. Then there are other issues that are more directly related to the programming language itself. These include topics such as data types and control flow constructs. This chapter discusses both types of beginning issues.

Preparing Your Perl Program

For modest programs such as the ones that we demonstrate in this chapter, the first step is to prepare the source code for your program. In order to do this, you can use your favorite text editor. On your Windows machine, you might use Notepad or Edit. The choice of editor is yours. Just be certain that you do not use a word processor such as Microsoft Word. Word processors insert special codes into your files, whereas editors do not. These special codes interfere with the subsequent translation of your source code.

If you have Visual Studio and Visual Perl (see Appendix A), you can use this friendly integrated development environment (IDE). For the convenience of Visual Perl users, we provide solution (.sln) and project (.perlproj) files for each Perl program. We store each Perl program in its own folder within the chapter directory. Thus, the Perl programs in this chapter are in the Chap02 folder.

The name of the file that you create—the name of your Perl program—is totally up to you. There are no real conventions in the Perl community about what to name Perl source files. However, we always use files whose names end with the extension .pl.

Viewing Your Source Code

Once you have typed your code into your editor and saved your file, you may wish from time to time to view your source file. In a DOS window, you can use the type command. Or, if the file contains more lines than you can fit on your display, you can use the more command. Often in this section of the book we will want to display a file so that you can compare the results of a program with the code that produced it. We use the type command to display the file. Also, we always use a mythical % as the system prompt.

Observations from a Small Perl Program

Let's say that you have entered a small Perl program into a file named small.pl. See the folder Small.


% type small.pl
#
#       small.pl
#
$name = "Michael";              # a string
$id = 1;                        # an integer
print "Name is	$name
";
print "Id is	$id
";
%

There are several things to notice in the program above. The # character is the comment character. The Perl translator ignores it and everything else up to and including the end of the line character. It is the place for the programmer to add explanations about the working of the code.

Each Perl statement must end in a semicolon. The semicolon is a statement separator, not a statement terminator. The last line of the program does not need to end with a semicolon, but it is a good idea to place one there anyway.

The program above is simple enough that before we explain it further, we wish to show you how to execute it.

Executing a Perl Program

Executing a Perl program is a simple matter. Give the name of the Perl command on the command line and follow it by the name of the file you wish to execute. See the folder Demo.


% perl demo.pl
Name is   Michael
Id is     1
%

Unlike compiled languages, such as C and C++, Perl does not produce any object files—that is, intermediate files that need to be linked to produce an executable. Rather, your program is translated and executed as the Perl interpreter reads it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.171.136