Perl CGI: Perl Script Basics
Prepared by Bill Kilgallon,
Bill@KilgallonFamily.com
- A "#" character tells Perl that the rest of the line is simply a
comment, and should be ignored and not executed.
- Apache will look at the first line of a program and look for a
"#!" to appear in the first and second column. If it sees them, it
uses the rest of the line as the name of the interpreter to be used to
execute the remainder of the file.
- On Unix systems, this usually looks something like
"#!/usr/bin/perl". On Windows systems, it looks more like
"#!/Perl/bin/perl.exe". On your system, it should be a "#", followed
by a "!", followed by the full path to the perl.exe program on your
system (you can use "start"->"find files or folders" to try and find
this executable).
- This line is called the "shebang" line.
- You can supply arguments on this line. A "-w" argument to Perl
is highly encouraged, it tells Perl to generate very complete and very
specific warnings when you are doing "bad" things. It significantly
simplifies debugging.
- If you want to import extra functionality from supplied libraries
and use them in your program, you do so with a directive named "use".
For example, adding the directive "use strict;" to your Perl program
tells it to issue a complaint if you try to use a variable you have
not declared. This prevents typo's in variable names from creating
exceptionally difficult to debug errors.
- The "use CGI;" directive loads all sorts of powerfull and easy
ways to generate HTML output on the fly.
- You can also run a perl program yourself without the browser. If
you are using the "use CGI;" directive, you will have an opportunity
to enter name value pairs. These will be explained later, for now
just hit Ctrl-Z (ctrl-d on unix) to continue on to running your
program.
- You can also run your program by double clicking it, but it may
come and go so fast you can't see the output. To make it wait, enter
the line "my $wait = <STDIN>;" at the end of your program. It will
remain running until you hit return. Note that you have to remember
to remove this line before it can be run sucessfully by the web
server.
- Finally (the whole point really) is that you can run your script
with your server by means of a web broswer. Point the browser to your
hostname, tell it to use the http protocol, and give it a path to your
file. For a default Apache installation on a windows box, the CGI
files should be placed in (and will run from) "C:/Program
Sources/Apache Group/Apache/cgi-bin". To run a file in this directory
named "counter.pl", you would use the URL
"http://localhost/cgi-bin/counter.pl".
Return to Index