[Top] [Prev] [Next] [Bottom]

3 Developing FastCGI
Applications in Perl

This chapter explains how to code FastCGI applications in Perl. Before you can build FastCGI applications in Perl, you must have a FastCGI-savvy version of the Perl interpreter. Open Market develops such Perl binaries for popular platforms and makes them available with our developer's kit.

The FastCGI-savvy binaries are extensions of standard Perl, and are intended to replace your existing Perl installation. There is no need to maintain two versions of Perl: the version that we supply will work fine when invoked from a shell or a CGI program. There are also directions in the developer's kit for how to make your own FastCGI-savvy Perl, if you need a version for some platform that we don't supply.

FastCGI is ideal for applications written in Perl, because it provides a huge performance gain. When you run a Perl script, the Perl interpreter analyzes the entire script before executing any of it. With FastCGI, you can factor out this initialization cost and pay it only once, making execution of the actual script much faster in response to client calls.



Getting Started

The first line of any Perl script typically specifies the pathname of the Perl interpreter itself. You must specify the pathname of a FastCGI-savvy Perl.

Next, you must tell Perl to load the FastCGI extension. To do so, place the following line near the beginning of every FastCGI script:



use FCGI;

Then, you have to divide FastCGI scripts into the following two sections:



A response loop typically has the following format:



while (FCGI::accept >= 0) {

# body of response loop

}

The accept call returns 0 whenever a client requests the FastCGI script. Otherwise, the accept call returns -1.



Example: TinyFastCGI

Here is a simple example of a FastCGI application written in Perl:




#!fcgi-savvy-perl

use FCGI; # Imports the library; required line

# Initialization code

$cnt = 0;

# Response loop

while (FCGI::accept >= 0) {
  print "Content-type: text/html\r\n\r\n";
  print "<head>\n<title>FastCGI Demo Page (perl)</title>\n</head>\n";
  print  "<h1>FastCGI Demo Page (perl)</h1>\n";
  print "This is coming from a FastCGI server.\n<BR>\n";
  print "Running on <EM>$ENV{SERVER_NAME}</EM> to <EM>$ENV{REMOTE_HOST}</EM>\n<BR>\n";
   $cnt++;
  print "This is connection number $cnt\n";
}



[Top] [Prev] [Next] [Bottom]