![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
First Things First: Perls before CodeYou cant do anything without the Perl language interpreter. Make sure you have a copy of it before you go further or youll get snotty error messages from whatever operating system youre using.
Ill 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 thats the case on your system, obviously you dont have to do anything. The Perl executable for NT and Windows 95 can be downloaded for nothing from Microsofts Web site and several others. It performs flawlessly.
Loading the InterpreterRegardless of your operating system, once you have the Perl interpreter, youre 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 couldnt be found. The same is true in Windows NT and Windows 95.
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
Thats fairly easy, isnt it? Well explain whats going on in the next section; for now, save the file as hello.pl (hello.pl in quotes if youre using Notepad on Windows 95 or NT) and close your text editor. Running the Hello ExampleThe 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.
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 wont 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, youll 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.
Notice that the program prints Hello, World! with a line-ender to the screen. Congratulations! You are now a Perl programmer.
|
![]() |
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. |