CGI/Perl Basics

by Steven J. Owens (unless otherwise attributed)

CGI/Perl Basics

The basic requirements to have and run a CGI/Perl script in a typical webserver (e.g. apache on some variety of unix) are:

  • 1) execute permission enabled on the script file
  • 2) located cgi-bin directory and/or has proper suffix
  • 3) has proper #! line
  • 4) code is syntactically correct, compiles cleanly
  • 5) imports CGI module
  • 6) outputs intelligible HTML

    To go into these in a bit more detail:

    1) execute permission enabled on the script file

    Most web servers are run on unix-flavored operating systems. This means that they generally won't let you run a file as a program unless you explicitly tell the operating system that file is meant to be executed, using the chmod command. For a really quickie intro to this topic, read

    2) located cgi-bin directory and/or has proper suffix

    Now that the operating system knows it's all right to run your file, you need to make sure the web server knows it's all right. How you do this depends on your web server and on the configuration choices your system administrator made when he or she installed your web server.

    With Apache, the most popular web server, you typically do this either by putting the file somewhere under a special directory, typically named "cgi-bin", or by giving the file a specific suffix, typically ".cgi", like "foo.cgi".

    This is controlled by the httpd.conf file, which is typically located somewhere like "/etc/apache/httpd.conf". The keywords to search for are "ScriptAlias" and "handler". The ScriptAlias directive controls which directories are allowed to contain scripts, while handlers are the general mechanism for giving a file special treatment depending on its suffix.

    3) has proper #! line

    Executable files in Unix have to contain either machine code, or some sort of pseudo-english "scripting" language code. If it's a script, the first line has to be something like:


    The operating system uses this to figure out what program to run to interpret the commands and on-the-fly translate them into machine operations. The key things are to make sure you have this line and to make sure that the interpreter it points it is really there. You also might want to check and make sure that version of perl is correct (by entering "/usr/bin/perl -v" from the command line) for the script you're running.

    4) code is syntactically correct, compiles cleanly

    For perl, you can test this by executing the script with the "-c" option, which tells perl to make sure it compiles cleanly, but not actually run it.

    5) imports the module (use CGI;)

    If your code just does something really simple, that's all you need, for example it could just contain:

    print "<HTML>\n<BODY BGCOLOR=#FFFFFF>\n<H1>Hello World!</H1>\n</BODY>\n</HTML>\n" ;

    However, most CGI scripts need to parse incoming CGI arguments and do other sorts of things. To do this, you use the CGI module. If you're using pre-baked scripts somebody else wrote, this just means you have to make sure your installation has a copy of whatever flavor of the CGI module they used. This is a lot easier these days, since is part of perl 5 and is what most people use for this sort of stuff.

    6) outputs intelligible HTML

    Lastly, if you've done all of the above and your browser isn't showing you anything, use your browser's "View source" command to look at the source of the web page you're seeing - maybe your script is returning broken HTML and your browser isn't displaying it properly.

    See original (unformatted) article


    Verification Image:
    Your Email Address:
    Confirm Address:
    Please Post:
    Copyright: By checking the "Please Post" checkbox you agree to having your feedback posted on notablog if the administrator decides it is appropriate content, and grant compilation copyright rights to the administrator.
    Message Content: