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


First Things First: Perls before Code

You can’t do anything without the Perl language interpreter. Make sure you have a copy of it before you go further or you’ll get snotty error messages from whatever operating system you’re using.


TIP:  You can get Perl for Win32—Windows NT and Windows 95—by pointing your Web browser to http://www.activeware.com. UNIX sources for Perl are numerous and ever-changing; the best way to find them is in the Internet newsgroups at comp.sources.unix. MacPERL for the Macintosh is available at www.iis.ee .ethz.ch/~neeri/macintosh/perl.html.

I’ll make very few assumptions about the computer you are using or the operating system that it runs. However, most of the really good Perls that can be obtained are intended to run on Windows NT and Windows 95 or UNIX and most of our examples will emphasize those two platforms.

Installing the Perl interpreter can be as simple as running a setup program, or as complicated as extracting the source code and compiling it yourself.

Fortunately, Perl is included in many UNIX distributions these days. If that’s the case on your system, obviously you don’t have to do anything. The Perl executable for NT and Windows 95 can be downloaded for nothing from Microsoft’s Web site and several others. It performs flawlessly.

Compiling Your Own Perl Interpreter…

Compiling the Perl source code yourself is the method preferred by UNIX system administrators, who usually have a rather macho attitude about such things. Because the most freely available C code for Perl was written primarily for UNIX systems, it compiles easily most of the time.

Likewise, compiling the code for Windows NT and Windows 95 is possible, but only for the most daring of systems gurus; the process certainly is beyond the scope of this book. The latest versions of the most popular C/C++ development packages available for Windows—Visual C++ from Microsoft and Borland C++ for Windows from Borland International—both contain quirks that prevent a straightforward compilation of the Perl source code. Unless you want to change the functionality of Perl (which is an exercise of dubious logical value in itself) and devote hours to debugging someone else’s code, you’re much better off simply using whatever executable files you can find for the operating system you’re using.

Loading the Interpreter

Regardless of your operating system, once you have the Perl interpreter, you’re ready to go. On UNIX, things will be a little easier if you put the Perl interpreter in a subdirectory that is included in your PATH environment string, which is a system variable that maps out where the operating system should look when you type the name of a program at the command line. In other words, if you have loaded PATH by typing PATH=/usr/bin;/usr/me;/pub/local/etc at the command line and you then enter perl, the operating system will look in each of those directories for Perl before it gives up and complains to you that the command couldn’t be found. The same is true in Windows NT and Windows 95.


TIP:  The setup program for the Win32 Perl at www.activeware.com will ask you if you want Perl to be added to your PATH. If you answer affirmatively, the change will take place the next time to restart your computer.

As we discussed earlier, Perl scripts are simple text files that you can create using your favorite text editor. To put together your first Perl program, start that text editor now and enter the following lines:

   #!/usr/bin/perl

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

   #           End hello.pl


NOTE:  The first line in the program begins with Perl’s “comment” character (#) and it will be ignored by the interpreter. However, it must contain the path to your Perl interpreter. If your system’s Perl interpreter is not in /usr/bin, change the path to the correct subdirectory.

That’s fairly easy, isn’t it? We’ll explain what’s going on in the next section; for now, save the file as hello.pl (“hello.pl” in quotes if you’re using Notepad on Windows 95 or NT) and close your text editor.

Running the “Hello” Example

The hello.pl is about as tiny as programs get, both in the writing and in the execution. It is intended to be run from the command line, which means the shell in UNIX, the console command processor in Windows NT, or CMD.EXE or COMMAND.COM in Windows 95.


TIP:  The term “command line” will be used frequently in this book, so to avoid the confusion of having to refer to both operating system methods, we’ll henceforth refer to the UNIX shell and the NT/95 console as the “command line.” Also, because Perl adheres to the UNIX convention of specifying path names with the forward slash (/) rather than Microsoft’s backslash (\) we will adhere to it, too, in the text of our examples. Remember the difference when you’re typing commands in the NT console.

Open a command-line window (a shell in UNIX, a command console or MS-DOS window in Windows 95 and NT) Because Perl is an interpreted language, you won’t be running your first Perl program directly. You have to run perl with your Perl program as an argument to it. If, when you installed the Perl software on your system, you did as we suggested and put it somewhere in your PATH, then you can simply type:

   perl hello.pl

Otherwise, you’ll have to type in the full path to perl followed by the name of your program. For example, if you installed Perl in /myprogs/perl, and that subdirectory is not in your PATH environment variable, you would have to type:

   /myprogs/perl hello.pl

In any event, when you run the program, the result should look something like Figure 1.5.


Figure 1.5:  The results of running your first Perl program

Notice that the program prints “Hello, World!” with a line-ender to the screen.

Congratulations! You are now a Perl programmer.


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.