next up previous contents
Next: Transmitting Information through CGI Up: Perl on the Web Previous: Perl on the Web   Contents

Introduction

When we sit in front of a web browser, and type in a URL, like www.yahoo.com, we're really asking a computer on the internet, known as www.yahoo.com, to send us some information corresponding to a set of rules known as the hypertext transport protocol (HTTP). Our browser understands this protocol, and displays text, pictures, fill-out forms, or whatever else the designers of the web page have put in the file which was sent to us. (When you specify a name without an explicit filename at the end, the web server you're talking to defaults to sending a file with a particular name, like index.html, so even when you don't actually specify a file name, the web server knows what file to send you.) The rules for writing such files so that they look pretty on our web browser is known as HTML (hypertext markup language). For many of the pages we view, the information we receive is static, that is, there is a file sitting on a webserver somewhere, and regardless of who we are or what we do, it sends us the same content.

An alternative to this scheme is to specify a URL which is a program. When we point our web browser to such a program, it can do a wide variety of things -- in fact anything that the programmer who wrote it wanted it to do. The standard output stream from the program is sent to your web browser; thus, if you have a program which prints valid HTML code, you can generate dynamic information on the fly. Your program doesn't need to decide what to print until it receives a request, and then it can send back whatever it deems suitable.

If this communication was one way, that is if all you could tell the web server to do is to run a program, without providing it any additional information, this would probably be more or less just a curiousity. But this method of programming for the web does have a way of transmitting information from the browser to the webserver, and the protocol for doing it is known as CGI (Common Gateway Interface) programming. CGI refers to, among other things, the way that information can be extracted from, say, a fill-out form or drop down menu which is displayed in your browser.

Unlike some internet protocols, HTTP was designed to be extensible - you can even provide information that was not conceived of when the protocol was designed. It does this in a very simple way; when information is transmitted from a web server, it is preceded by a header, which explains to the browser the type of the content, the length, and just about anything else that anyone would like to communicate. Similarly, the web browser can inform the web server about what type of browser you're using, what types of content you'll accept, and other similar information, by constructing appropriate header lines. By using two newlines with no intervening characters as the termination of the headers, the protocol truly allows unlimited information to be transmitted. When you write a static web page, the web server takes care of providing the appropriate headers; when you are writing CGI programs, it becomes your responsibility. Fortunately for perl programmers, the CGI module not only takes care of details like the headers, it also allows you to create simple web pages without knowing anything about HTML, and more complex ones with only a small amount of HTML knowledge.

Before you write a perl program and place it on a webserver, you should realize that, because of security reasons, CGI programs can only be executed from certain restricted directories within the file system of the web server. If you just place a perl program in, say a public_html location on a webserver, it will display as text, which is rarely what you really want. So one of the first steps in CGI programming is to either set up a personal web server, or to inquire from your local webmaster how to gain access to a directory which is authorized to allow execution of programs. If this is not possible, you can still develop CGI programs, using the CGI module's offline mode (Section [*]).


next up previous contents
Next: Transmitting Information through CGI Up: Perl on the Web Previous: Perl on the Web   Contents
Phil Spector 2002-10-18