1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
5 FastCGI Programmer's Guide - Chapter 3, Developing FastCGI Applications in Perl
7 <STYLE TYPE="text/css">
9 background-color: #ffffff;
11 li.c2 {list-style: none}
12 div.c1 {text-align: center}
16 <A HREF="cover.htm">[Top]</A> <A HREF="ch2c.htm">[Prev]</A> <A HREF="ch4tcl.htm">[Next]</A> <A HREF=
17 "ap_guida.htm">[Bottom]</A>
23 3 Developing FastCGI<BR>
29 This chapter explains how to code FastCGI applications in Perl. Before you can build FastCGI applications in
30 Perl, you must have a FastCGI-savvy version of the Perl interpreter. Open Market develops such Perl binaries
31 for popular platforms and makes them available with our developer's kit.
34 <A NAME="5008"></A> The FastCGI-savvy binaries are extensions of standard Perl, and are intended to replace
35 your existing Perl installation. There is no need to maintain two versions of Perl: the version that we supply
36 will work fine when invoked from a shell or a CGI program. There are also directions in the developer's
37 kit for how to make your own FastCGI-savvy Perl, if you need a version for some platform that we don't
41 <A NAME="4369"></A> FastCGI is ideal for applications written in Perl, because it provides a huge performance
42 gain. When you run a Perl script, the Perl interpreter analyzes the entire script before executing any of it.
43 With FastCGI, you can factor out this initialization cost and pay it only once, making execution of the actual
44 script much faster in response to client calls.
53 The first line of any Perl script typically specifies the pathname of the Perl interpreter itself. You must
54 specify the pathname of a FastCGI-savvy Perl.
57 <A NAME="4235"></A> Next, you must tell Perl to load the FastCGI extension. To do so, place the following line
58 near the beginning of every FastCGI script:
63 <A NAME="4210">use FCGI;
68 Then, you have to divide FastCGI scripts into the following two sections:
77 Initialization section, which is executed only once. <A NAME="4243"></A>
80 Response loop section, which gets executed every time the FastCGI script gets called.
85 A response loop typically has the following format:
90 <A NAME="4255">while (FCGI::accept >= 0) {
92 <A NAME="4256"># body of response loop
99 The <CODE>accept</CODE> call returns 0 whenever a client requests the FastCGI script. Otherwise, the
100 <CODE>accept</CODE> call returns -1.
109 Here is a simple example of a FastCGI application written in Perl:
117 use FCGI; # Imports the library; required line
119 # Initialization code
125 while (FCGI::accept >= 0) {
126 print "Content-type: text/html\r\n\r\n";
127 print "<head>\n<title>FastCGI Demo Page (perl)</title>\n</head>\n";
128 print "<h1>FastCGI Demo Page (perl)</h1>\n";
129 print "This is coming from a FastCGI server.\n<BR>\n";
130 print "Running on <EM>$ENV{SERVER_NAME}</EM> to <EM>$ENV{REMOTE_HOST}</EM>\n<BR>\n";
132 print "This is connection number $cnt\n";
139 <A HREF="cover.htm">[Top]</A> <A HREF="ch2c.htm">[Prev]</A> <A HREF="ch4tcl.htm">[Next]</A> <A HREF=
140 "ap_guida.htm">[Bottom]</A>
143 <!-- This file was created with Quadralay WebWorks Publisher 3.0.3 -->
145 <!-- For more information on how this document, and how the rest of -->
146 <!-- this server was created, email yourEmail@xyzcorp.com -->
148 <!-- Last updated: 04/15/96 08:00:18 -->