ch3perl.htm   [plain text]


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
   <HEAD>
      <TITLE>
         FastCGI Programmer&#39;s Guide - Chapter 3, Developing FastCGI Applications in Perl
      </TITLE>
<STYLE TYPE="text/css">
 body {
  background-color: #ffffff;
 }
 li.c2 {list-style: none}
 div.c1 {text-align: center}
</STYLE>
   </HEAD>
   <BODY>
      <A HREF="cover.htm">[Top]</A> <A HREF="ch2c.htm">[Prev]</A> <A HREF="ch4tcl.htm">[Next]</A> <A HREF=
      "ap_guida.htm">[Bottom]</A> 
      <HR>
      <BR>
       <A NAME="3659"></A>
      <DIV CLASS="c1">
         <H1>
            3 Developing FastCGI<BR>
            Applications in Perl
         </H1>
      </DIV>
      <A NAME="917"></A>
      <P>
         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&#39;s kit.
      </P>
      <P>
         <A NAME="5008"></A> 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&#39;s
         kit for how to make your own FastCGI-savvy Perl, if you need a version for some platform that we don&#39;t
         supply.
      </P>
      <P>
         <A NAME="4369"></A> 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.
      </P>
      <BR>
      <BR>
      <H1>
         Getting Started
      </H1>
      <A NAME="4234"></A>
      <P>
         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.
      </P>
      <P>
         <A NAME="4235"></A> Next, you must tell Perl to load the FastCGI extension. To do so, place the following line
         near the beginning of every FastCGI script:
      </P>
      <BR>
      <BR>
<PRE>
<A NAME="4210">use FCGI;
</A>
</PRE>
      <A NAME="4212"></A>
      <P>
         Then, you have to divide FastCGI scripts into the following two sections:
      </P>
      <BR>
      <BR>
      <UL>
         <LI CLASS="c2">
            <A NAME="4242"></A>
         </LI>
         <LI>
            Initialization section, which is executed only once. <A NAME="4243"></A>
         </LI>
         <LI>
            Response loop section, which gets executed every time the FastCGI script gets called.
         </LI>
      </UL>
      <A NAME="4248"></A>
      <P>
         A response loop typically has the following format:
      </P>
      <BR>
      <BR>
<PRE>
<A NAME="4255">while (FCGI::accept &gt;= 0) {
</A>
<A NAME="4256"># body of response loop
</A>
<A NAME="4257">}
</A>
</PRE>
      <A NAME="4258"></A>
      <P>
         The <CODE>accept</CODE> call returns 0 whenever a client requests the FastCGI script. Otherwise, the
         <CODE>accept</CODE> call returns -1.
      </P>
      <BR>
      <BR>
      <H1>
         Example: TinyFastCGI
      </H1>
      <A NAME="4588"></A>
      <P>
         Here is a simple example of a FastCGI application written in Perl:
      </P>
      <BR>
      <BR>
<PRE>
<A NAME="4589"></A>
#!fcgi-savvy-perl

use FCGI; # Imports the library; required line

# Initialization code

$cnt = 0;

# Response loop

while (FCGI::accept &gt;= 0) {
  print &quot;Content-type: text/html\r\n\r\n&quot;;
  print &quot;&lt;head&gt;\n&lt;title&gt;FastCGI Demo Page (perl)&lt;/title&gt;\n&lt;/head&gt;\n&quot;;
  print  &quot;&lt;h1&gt;FastCGI Demo Page (perl)&lt;/h1&gt;\n&quot;;
  print &quot;This is coming from a FastCGI server.\n&lt;BR&gt;\n&quot;;
  print &quot;Running on &lt;EM&gt;$ENV{SERVER_NAME}&lt;/EM&gt; to &lt;EM&gt;$ENV{REMOTE_HOST}&lt;/EM&gt;\n&lt;BR&gt;\n&quot;;
   $cnt++;
  print &quot;This is connection number $cnt\n&quot;;
}
</PRE>
      <P>
      </P>
      <HR>
      <BR>
       <A HREF="cover.htm">[Top]</A> <A HREF="ch2c.htm">[Prev]</A> <A HREF="ch4tcl.htm">[Next]</A> <A HREF=
      "ap_guida.htm">[Bottom]</A> 
      <HR>
      <BR>
       <!-- This file was created with Quadralay WebWorks Publisher 3.0.3 -->
      <!-- -->
      <!-- For more information on how this document, and how the rest of -->
      <!-- this server was created, email yourEmail@xyzcorp.com -->
      <!-- -->
      <!-- Last updated: 04/15/96 08:00:18 -->
   </BODY>
</HTML>