Add the .exe extension when appropriate to the clean target.
[catagits/fcgi2.git] / doc / fastcgi-prog-guide / ch3perl.htm
CommitLineData
6791223e 1<html><head><title>FastCGI Programmer's Guide - Chapter 3, Developing FastCGI Applications in Perl</title></head>
0198fd3c 2<body bgcolor=#ffffff>
3
4<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>
5<hr><br>
6
7<a name="3659">
8<center><h1>3 Developing FastCGI <br>Applications in Perl</h1></center>
6791223e 9</a><a name="917"></a>
0198fd3c 10This 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. <p>
6791223e 11<a name="5008"></a>
0198fd3c 12The 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.<p>
6791223e 13<a name="4369"></a>
0198fd3c 14FastCGI 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>
6791223e 15<a name="4183">
0198fd3c 16<h1> Getting Started</h1>
6791223e 17</a><a name="4234"></a>
0198fd3c 18The 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>
6791223e 19<a name="4235"></a>
0198fd3c 20Next, you must tell Perl to load the FastCGI extension. To do so, place the following line near the beginning of every FastCGI script: <p>
6791223e 21<pre><a name="4210">
0198fd3c 22use FCGI;
23</a>
6791223e 24</pre><a name="4212"></a>
0198fd3c 25Then, you have to divide FastCGI scripts into the following two sections:<p>
6791223e 26<ul><a name="4242"></a>
0198fd3c 27<li>Initialization section, which is executed only once.
6791223e 28<a name="4243"></a>
0198fd3c 29<li>Response loop section, which gets executed every time the FastCGI script gets called.
6791223e 30</ul><a name="4248"></a>
0198fd3c 31A response loop typically has the following format:<p>
6791223e 32<pre><a name="4255">
0198fd3c 33while (FCGI::accept &gt;= 0) {
34</a>
35<a name="4256">
36# body of response loop
37</a>
38<a name="4257">
39}
40</a>
6791223e 41</pre><a name="4258"></a>
0198fd3c 42The <code>accept</code> call returns 0 whenever a client requests the FastCGI script. Otherwise, the <code>accept</code> call returns -1. <p>
6791223e 43<a name="5002">
0198fd3c 44<h1> Example: TinyFastCGI</h1>
6791223e 45</a><a name="4588"></a>
0198fd3c 46Here is a simple example of a FastCGI application written in Perl:<p>
6791223e 47<pre><a name="4589"></a>
0198fd3c 48#!fcgi-savvy-perl
49
50use FCGI; # Imports the library; required line
51
52# Initialization code
53
54$cnt = 0;
55
56# Response loop
57
58while (FCGI::accept &gt;= 0) {
59 print "Content-type: text/html\r\n\r\n";
60 print "&lt;head&gt;\n&lt;title&gt;FastCGI Demo Page (perl)&lt;/title&gt;\n&lt;/head&gt;\n";
61 print "&lt;h1&gt;FastCGI Demo Page (perl)&lt;/h1&gt;\n";
62 print "This is coming from a FastCGI server.\n&lt;BR&gt;\n";
63 print "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";
64 $cnt++;
65 print "This is connection number $cnt\n";
66}
67</pre>
68<p>
6791223e 69
0198fd3c 70<hr><br>
71
72<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>
73<hr><br>
74
75
76
77
78<!-- This file was created with Quadralay WebWorks Publisher 3.0.3 -->
79<!-- -->
80<!-- For more information on how this document, and how the rest of -->
81<!-- this server was created, email yourEmail@xyzcorp.com -->
82<!-- -->
83<!-- Last updated: 04/15/96 08:00:18 -->
84
85</body>
86</html>