Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Perl CGl Programming: No experience required.
(Publisher: Sybex, Inc.)
Author(s): Erik Strom
ISBN: 0782121578
Publication Date: 11/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


How Perl Programs Run

In a technical sense, the Perl interpreter is a language compiler that doesn’t write its translated output to a file on the disk. Its “output file” is the screen, which is called standard output or stdout in systems parlance.

If a program name is given on the command line, the interpreter first checks the validity of each line, dumping out error messages for incorrect code and stopping if it finds any. If your program passes muster, the interpreter executes each of its lines of code.

One of the convenient aspects of doing it this way is that you find out immediately if your program does something wrong—and programs inevitably do! Most developers work on “windowed” systems, and they run the text editor with their Perl program code in one window and keep the command-line screen in another (see Figure 1.6). It is then quite easy to pop from window to window, writing and fixing code in the text editor and testing the code from the command line. With Perl, you get all your errors at once, and that speeds up the coding process. With a compiled language such as C or C++, you have to write the code, compile it, fix any errors that have cropped up in the compilation, compile it again, link it to the external libraries it needs, then—whew!—run it and see what errors occur there. Then you get to start all over again. It’s little wonder that Perl has become so popular!


Figure 1.6:  The two-window debugging process

Dissecting the “Hello” Example

We have briefly covered the first line in the program. We’ll now take apart this line, #!/usr/bin/perl, piece by piece:

# (pound sign) This is Perl’s “comment” character, which means that anything following it up to the end of the line is ignored by the interpreter. This is where you can document your program, so that others (or you after you haven’t touched the program in a few months!) can understand what is being accomplished in the code.
! (exclamation point) This first comment line is a special case. UNIX aficionados will read to the exclamation point (!) and recognize it as an instruction to the shell; a command for the command line. Strictly speaking, this tells the shell to run the Perl interpreter with the program code as its input.


NOTE:  The first line is required, and it should always contain the full path to your Perl interpreter, which may or may not be in /usr/bin. I used /usr/bin as an example because it is a common place to put it. Oddly, though there is no direct way under Windows 95 or NT to run a command with the ! character, the Win32 Perl interpreter requires you to follow this convention. You will get an error message if the path is specified incorrectly.

The Heart of the Program: print

We have used only one real Perl function in this short program—print. This function is a real workhorse, especially in Web programming, where you will use Perl to construct HTML pages. print is a function that you very likely will use in every program you write.

How does print work? We’ll go into a detailed description later, because print can do a lot. For now, let’s look at what it does in hello.pl:

   print "Hello, World!", "\n";

The unadorned print, as we have used it in the example program, takes a list of strings; that is, text enclosed by quotation marks, as its arguments, or parameters.


TIP:  The terms argument and parameter will be used interchangeably throughout this book in reference to the data you will use with Perl functions.

In this case, we are telling print that we want it to “print” the phrases “Hello World!” and “\n” to the screen. Notice that the two phrases, which are the print function’s arguments, are separated by a comma. It is also important that the line ends with a semicolon. All code lines in Perl must end with the semicolon; the interpreter will complain bitterly if you forget to do this, and it’s usually the first thing you will do wrong. Be forewarned!


WARNING:  All code lines in Perl must end in a semicolon. Why? The interpreter can’t decide for itself where a code statement ends, because it may extend for more than one line. The semicolon tells the interpreter, “This statement ends here.”


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.