remove dos eols
[catagits/fcgi2.git] / doc / fastcgi-prog-guide / ch3perl.htm
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
2 <HTML>
3    <HEAD>
4       <TITLE>
5          FastCGI Programmer&#39;s Guide - Chapter 3, Developing FastCGI Applications in Perl
6       </TITLE>
7 <STYLE TYPE="text/css">
8  body {
9   background-color: #ffffff;
10  }
11  li.c2 {list-style: none}
12  div.c1 {text-align: center}
13 </STYLE>
14    </HEAD>
15    <BODY>
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> 
18       <HR>
19       <BR>
20        <A NAME="3659"></A>
21       <DIV CLASS="c1">
22          <H1>
23             3 Developing FastCGI<BR>
24             Applications in Perl
25          </H1>
26       </DIV>
27       <A NAME="917"></A>
28       <P>
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&#39;s kit.
32       </P>
33       <P>
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&#39;s
37          kit for how to make your own FastCGI-savvy Perl, if you need a version for some platform that we don&#39;t
38          supply.
39       </P>
40       <P>
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.
45       </P>
46       <BR>
47       <BR>
48       <H1>
49          Getting Started
50       </H1>
51       <A NAME="4234"></A>
52       <P>
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.
55       </P>
56       <P>
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:
59       </P>
60       <BR>
61       <BR>
62 <PRE>
63 <A NAME="4210">use FCGI;
64 </A>
65 </PRE>
66       <A NAME="4212"></A>
67       <P>
68          Then, you have to divide FastCGI scripts into the following two sections:
69       </P>
70       <BR>
71       <BR>
72       <UL>
73          <LI CLASS="c2">
74             <A NAME="4242"></A>
75          </LI>
76          <LI>
77             Initialization section, which is executed only once. <A NAME="4243"></A>
78          </LI>
79          <LI>
80             Response loop section, which gets executed every time the FastCGI script gets called.
81          </LI>
82       </UL>
83       <A NAME="4248"></A>
84       <P>
85          A response loop typically has the following format:
86       </P>
87       <BR>
88       <BR>
89 <PRE>
90 <A NAME="4255">while (FCGI::accept &gt;= 0) {
91 </A>
92 <A NAME="4256"># body of response loop
93 </A>
94 <A NAME="4257">}
95 </A>
96 </PRE>
97       <A NAME="4258"></A>
98       <P>
99          The <CODE>accept</CODE> call returns 0 whenever a client requests the FastCGI script. Otherwise, the
100          <CODE>accept</CODE> call returns -1.
101       </P>
102       <BR>
103       <BR>
104       <H1>
105          Example: TinyFastCGI
106       </H1>
107       <A NAME="4588"></A>
108       <P>
109          Here is a simple example of a FastCGI application written in Perl:
110       </P>
111       <BR>
112       <BR>
113 <PRE>
114 <A NAME="4589"></A>
115 #!fcgi-savvy-perl
116
117 use FCGI; # Imports the library; required line
118
119 # Initialization code
120
121 $cnt = 0;
122
123 # Response loop
124
125 while (FCGI::accept &gt;= 0) {
126   print &quot;Content-type: text/html\r\n\r\n&quot;;
127   print &quot;&lt;head&gt;\n&lt;title&gt;FastCGI Demo Page (perl)&lt;/title&gt;\n&lt;/head&gt;\n&quot;;
128   print  &quot;&lt;h1&gt;FastCGI Demo Page (perl)&lt;/h1&gt;\n&quot;;
129   print &quot;This is coming from a FastCGI server.\n&lt;BR&gt;\n&quot;;
130   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;;
131    $cnt++;
132   print &quot;This is connection number $cnt\n&quot;;
133 }
134 </PRE>
135       <P>
136       </P>
137       <HR>
138       <BR>
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> 
141       <HR>
142       <BR>
143        <!-- This file was created with Quadralay WebWorks Publisher 3.0.3 -->
144       <!-- -->
145       <!-- For more information on how this document, and how the rest of -->
146       <!-- this server was created, email yourEmail@xyzcorp.com -->
147       <!-- -->
148       <!-- Last updated: 04/15/96 08:00:18 -->
149    </BODY>
150 </HTML>
151