o small documentation fix
[catagits/fcgi2.git] / doc / fcgi-perl.htm
CommitLineData
0198fd3c 1<html>
2<head><title>Integrating FastCGI with Perl-5</title>
3</head>
4
5<body bgcolor="#FFFFFF" text="#000000" link="#cc0000" alink="#000011"
6vlink="#555555">
7
8<center>
9<a href="/fastcgi/words">
10 <img border=0 src="/kit/images/fcgi-hd.gif" alt="[[FastCGI]]"></a>
11</center>
12<br clear=all>
13<h3><center>Integrating FastCGI with Perl-5</center></h3>
14
15<!--Copyright (c) 1996 Open Market, Inc. -->
16<!--See the file "LICENSE.TERMS" for information on usage and redistribution-->
17<!--of this file, and for a DISCLAIMER OF ALL WARRANTIES. -->
18
19<h5 align=center>
20Copyright &copy; 1996 Open Market, Inc. 245 First Street, Cambridge,
21 MA 02142 U.S.A.<br>
22Tel: 617-949-7000 URL:
23 <a href="http://www.openmarket.com/">http://www.openmarket.com/</a><br>
24$Id: fcgi-perl.htm,v 1.1 1997/09/16 15:36:26 stanleyg Exp $ <br>
25</h5>
26<hr>
27
28<ul type=square>
29 <li><a HREF = "#S1">1. Introduction</a>
30 <li><a HREF = "#S2">2. Perl with sfio and an FCGI module</a>
31 <li><a HREF = "#S3">3. Perl with fcgi_stdio and an FCGI module</a>
32 <ul type=square>
33 <li><a HREF = "#S3.1">3.1 Basic recipe</a>
34 <li><a HREF = "#S3.2">3.2 Semi-advanced recipe</a>
35 <li><a HREF = "#S3.3">3.3 Advanced recipe</a>
36 </ul>
37 <li><a HREF = "#S4">4. Writing FastCGI applications in Perl</a>
38</ul>
39
40
41<H3><A NAME = "S1"> 1. Introduction</A></H3>
42Perl (Practical Extraction and Report Language) is a scripting language that
43is often used for CGI programming. Perl is freely available as a
44source kit.<p>
45
46FastCGI has been integrated with Perl in two different ways:
47<ol>
48 <li>By writing a module that plugs into any Perl interpreter that's
49 been built with sfio, a stdio alternative from AT&T.
50 <li>By writing a module that plugs into any Perl interpreter that's
51 been built with FastCGI's fcgi_stdio library
52 layering over stdio.
53</ul>
54The first approach, implemented by Sven Verdoolaege
55(skimo@breughel.ufsia.ac.be), is probably the better of the two,
56since sfio is a generally useful addition to Perl.
57The second approach, implemented by engineers at Open Market,
58predates the availability of an sfio-integrated
59Perl and demonstrates that the fcgi_stdio library
60can be used with a substantial C application.<p>
61
62The two approaches
63are compatible at the Perl source code level; a Perl
64application developed using
65one approach can be run using the other. And both approaches
66result in a general-purpose Perl interpreter, not a Perl interpreter
67that's only usable for FastCGI applications.<p>
68
69This memo documents both approaches and explains a small
70Perl FastCGI application.<p>
71
72
73<h3><a name ="S2"> 2. Perl with sfio and an FCGI module</a></h3>
74
75As of release 5 patch 3 subpatch 2 (5.003.02), Perl has announced an optional
76support for sfio (safe/fast string/file I/O), which is an alternative
77to stdio that AT&T distributes freely. An advantage of sfio over stdio
78is that sfio provides the ability to implement
79new stream classes that don't simply transfer sequential bytes to or from
80a file descriptor. This flexibility is exactly what FastCGI needs in order
81to implement the standard I/O streams in a manner that's
82transparent to applications.<p>
83
84Perl interpreters incorporating sfio are not widely available in
85binary form, so most likely you'll have to build your own.
86Your build should go smoothly if you follow the instructions
87below. The instructions assume:<p>
88
89<ul>
90 <li>You are building Perl 5.0 patch level 3 subpatch level 2 (5.003.02)
91 or higher. That's the first Perl release to support sfio.<p>
92</ul>
93<P>
94
95Follow these steps to build a Perl with sfio:<p>
96
97<ol>
98 <li>Obtain sfio source code from
99 <a href="ftp://ftp.funet.fi/pub/languages/perl/CPAN/src/misc">
100 ftp://ftp.funet.fi/pub/languages/perl/CPAN/src/misc</a><p>
101
102 <li>Unpack the tar file using <tt>tar xvf</tt> command. <EM>$sfio</EM>
103 will be used as a shorthand for the directory in which sfio package
104 is installed.<p>
105
106 <li>Update your $PATH variable as specified in <tt>$sfio/README</tt> and
107 run <tt>make</tt> command in the <tt>$sfio/src/lib/sfio</tt> subdirectory.<p>
108
109 <li>Rename or delete the file <tt>$sfio/include/stdio.h</tt>, since it may
110 interfere in the further build process.<p>
111
112 <li>Obtain Perl source (version 5 subversion 003 patchlevel 2 or higher) from
113 <a href="http://fohnix.metronet.com/perlinfo/source/5.0/unsupported">
114 http://fohnix.metronet.com/perlinfo/source/5.0/unsupported</a><p>
115
116 <li>Unpack the tar file using <tt>tar xvf</tt> command. <EM>$perl</EM> is
117 used as a shorthand for the directory that is created.<p>
118
119 <li>Configure, build, and install Perl as follows:
120
121<pre>
122% cd $perl
123% ./Configure -Duseperlio -Dusesfio
124% make
125% make test
126% make install
127</pre><p>
128
129There are certain Configure questions that must be answered
130differently when building Perl with sfio:<p>
131
132<DL>
133
134<DT><EM>Perl5 can now use alternate file IO mechanisms to ANSI stdio.
135However these are experimental and may cause problems with some
136extension modules.
137Use stdio as with previous versions? [y] </EM></DT>
138<DD>
139You should answer no.
140</DD><P>
141
142<DT><EM>Any additional cc flags?</EM></DT>
143<DD>
144You should use the following cc flags along with any defaults that Perl
145Configure supplied:
146<UL>
147<LI> <strong>-I<em>$sfio</em>/include</strong>
148</UL>
149</DD><P>
150
151<DT><EM>Any additional ld flags (NOT including libraries):</EM></DT>
152<DD>
153You should specify the following <tt>ld</tt> flags:
154<UL>
155<LI> <strong>-L<em>$sfio</em>/lib</strong>
156</UL>
157</DD><P>
158
159<DT><EM>Additional Libraries:</EM></DT>
160<DD>
161Check that <strong>-lsfio</strong> is one of the specified libraries. Press
162return key to continue.
163</DD><P>
164</DL>
165
166<b>NOTE</b>: If you did not install Perl as a root user, make sure to
167correctly set environment variable <tt>PERL5LIB</tt> to indicate the location
168of Perl libraries. For example, if you installed Perl binary into the
169<tt>$INSTALL</tt> subdirectory and you are running Solaris, the following
170will set your proper library path:
171<pre>
172% setenv PERL5LIB $INSTALL/lib:$INSTALL/lib/sun4-solaris/perl5.003_02
173</pre>
174</ul>
175<p>
176
177 <li>Obtain Perl/Sfio module for FastCGI support from
178 <a href="ftp://ftp.funet.fi/pub/languages/perl/CPAN/authors/id/SKIMO">
179 ftp://ftp.funet.fi/pub/languages/perl/CPAN/authors/id/SKIMO</a><p>
180
181 <li>Unpack FCGI module using <tt>tar</tt> command. We use <tt>$sfiomod</tt>
182 to denote the subdirectory that is created in the process.<p>
183
184 <li>Build and install the module with the following commands:
185<pre>
186% cd $sfiomod
187% $INSTALL/bin/perl Makefile.PL
188% make
189% make test
190% make install
191</pre>
192</ol>
193
194
195<H3><a NAME = "S3">3. Perl with fcgi_stdio and an FCGI module</a></H3>
196
197<H4><a NAME = "S3.1">3.1 Basic recipe</a></H4>
198
199Here are the assumptions embedded in the following recipe:
200<UL>
201
202<LI>You are building Perl 5.0 Patch 2 (5.002) or higher, since
203all examples that are provided are based on that release.
204<P></P>
205
206<LI>You have gcc version 2.7 installed on your system, and use it in the
207build. gcc is convenient because it supports the <tt>-include</tt>
208command-line option that instructs the C preprocessor to include a specific
209file before processing any other include files. This allows you to include
210<tt>fcgi_stdio.h</tt> without modifying Perl source files. (The reason for
211specifying gcc version 2.7 is that I have experienced bad behavior with an
212earlier version and the <tt>-include</tt> flag -- the C preprocessor died
213with SIGABRT.)
214<P></P>
215
216<LI> <EM>$fcgi</EM> is used as shorthand for the full path of the FastCGI
217developers kit.
218</UL>
219<P>
220If those are valid assumptions, follow these steps:
221<OL>
222<LI> Pull the Perl source kit from
223<A HREF="http://www.metronet.com/perlinfo/src/latest.tar.gz">
224http://www.metronet.com/perlinfo/src/latest.tar.gz</A>
225<P>
226There are good sources of information on Perl at:
227<UL>
228<LI> <A HREF="http://www.perl.com/">http://www.perl.com/</A>
229<LI> <A HREF="http://www.metronet.com/perlinfo/">http://www.metronet.com/perlinfo/</A>
230</UL>
231</P>
232</A>
233<LI> Unpack the tar file in the parent directory of the FastCGI kit
234directory, so that the perl directory is a sibling of <tt>fcgi-devel-kit</tt>.
235<EM>$perl</EM> is used as shorthand for the full path of the directory
236in which Perl is installed.
237<p>
238<LI> Copy the version specific and the common files from
239<tt>fcgi-devel-kit/perl-5</tt> into the Perl-5 kit.
240<PRE>
241> cd $perl
242> mv perl.c perl.c.orig
243> mv proto.h proto.h.orig
244> mv Configure Configure.orig
245> cp -r ../fcgi-devel-kit/perl-5/perl5.002/* .
246> cp -r ../fcgi-devel-kit/perl-5/common/* .
247</PRE>
248<P>
249The files you are copying contain the Perl-5 FCGI extension, some
250files modified from the distribution, and a script to simplify the
251configuration process.
252</P>
253<LI> Set environment variables.
254The Perl-5 FastCGI configuration process requires that the environment
255variable <TT>FCGIDIR</TT> be set to the top level directory of the FastCGI
256development kit.
257<PRE>
258> setenv FCGIDIR <EM>$fcgi</EM>
259</PRE>
260If you do not want to use <tt>gcc</tt> to build Perl you can set the
261environment variable <TT>CC</TT> to the desired compiler. For example:
262<PRE>
263> setenv CC gcc2.7
264</PRE>
265By default Perl's installation prefix is /usr/local, so binaries get
266installed in /usr/local/bin, library files get installed into
267/usr/local/lib/perl, etc. If you want to specify a different installation
268prefix set the environment variable <tt>PERL_PREFIX</tt>.
269<PRE>
270> setenv PERL_PREFIX /usr/local/perl5-fcgi
271</PRE>
272<LI> Run fcgi-configure.
273<PRE>
274> ./fcgi-configure
275</PRE>
276<P>
277<TT>fcgi-configure</TT> is a wrapper around Perl's <tt>Configure</tt> script.
278It sets some variables according the the value of some environment variables,
279and runs Perl's <tt>Configure</tt> script
280in such a way that it does not prompt the
281user for any input. 90% of the time this should work without a problem.
282If for some reason this does not work for you, you'll have to
283follow the steps in the next section.<p>
284<LI> Run make.
285<PRE>
286> make
287</PRE>
288<LI> Install the newly built Perl-5.
289<PRE>
290> make install
291</PRE>
292</OL><p>
293
294
295<H4><a NAME = "S3.2">3.2 Semi-advanced recipe</a></H4>
296
297If you do not have experience configuring and building Perl, you
298should find someone who does. Perl can be pretty intimidating to configure
299since it asks you a large number of irrelevant-seeming
300questions that you won't know how to answer.<p>
301<P>
302<OL>
303<LI>Go into the top level directory of the Perl distribution and run
304<tt>Configure</tt>.
305<PRE>
306> cd $perl
307> ./Configure
308</PRE>
309<LI>
310There are some questions that you are going to
311have to answer differently when building FastCGI into Perl.
312These are described below:
313<P></P>
314<DL>
315<DT><EM>Use which C compiler?</EM></DT>
316<DD>
317You should specify <tt>gcc</tt>.
318</DD>
319<P></P>
320<DT><EM>Any additional cc flags?</EM></DT>
321<DD>
322You should use the following cc flags along with any defaults that Perl
323Configure supplied:
324<UL>
325<LI> <strong>-I<em>$fcgi</em>/include</strong>
326<LI> <strong>-include <em>$fcgi</em>/include/fcgi_stdio.h</strong>
327</UL>
328This assumes you are using GCC.
329</DD>
330<P></P>
331
332<DT><EM>Any additional ld flags (NOT including libraries):</EM></DT>
333<DD>
334You should specify the following <tt>ld</tt> flags:
335<UL>
336<LI> <strong>-L<em>$fcgi</em>/libfcgi</strong>
337</UL
338</DD>
339<P></P>
340
341<DT><EM>Additional Libraries:</EM></DT>
342<DD>
343add <strong>-lfcgi</strong> to the list of additional libraries.
344It should be added before -lc.
345</DD>
346<P></P>
347
348<DT><EM>What extensions do you wish to load dynamically?</EM></DT>
349<DD>
350If you can support dynamic extensions, <tt>Configure</tt>
351will ask which of the
352supplied extensions should be loaded dynamically. Since we copied the FCGI
353extension into the Perl source directory it should be one of the ones in the
354default list. If you want FCGI to be dynamically loaded you should specify
355it here, otherwise leave it out.
356</DD>
357<P></P>
358
359<DT><EM>What extensions do you wish to load statically?</EM></DT>
360<DD>
361If you do not support Dynamic extensions this is the only question about
362extensions you would get asked. You should specify FCGI here if you did not
363get asked about dynamic extensions (or did not specify FCGI as a dynamic
364extension).
365</DD>
366</DL>
367<P></P>
368<LI> Copy in the new <tt>proto.h</tt>.
369<P>
370The file proto.h has some macros that conflict with the FastCGI macros.
371The version of <tt>proto.h</tt> supplied in the FastCGI kit
372includes these changes:<p>
373<UL>
374<LI> At the beginning of the file it adds the following lines:
375<PRE>
376#ifdef _FCGI_STDIO
377#undef printf
378#endif
379</PRE>
380<LI> At the bottom it adds:
381<PRE>
382#ifdef _FCGI_STDIO
383#define printf FCGI_printf
384#endif
385</PRE>
386</UL>
387<LI> Copy in the new <tt>perl.c</tt>.
388<P>
389Perl-5.002 has a bug in <tt>perl.c</tt> that has a great
390chance of getting exercised
391with FastCGI. A fix has been sumbitted to the Perl developers and hopefully
392it'll make it into perl-5.003. It was a one line fix, here is a diff for the
393curious:
394<PRE>
395*** perl.c 1996/03/15 17:10:10 1.1
396--- perl.c 1996/03/15 17:11:23
397***************
398*** 405,410 ****
399--- 405,411 ----
400 if (e_fp) {
401 if (Fflush(e_fp) || ferror(e_fp) || fclose(e_fp))
402 croak("Can't write to temp file for -e: %s", Strerror(errno));
403+ e_fp = Nullfp;
404 argc++,argv--;
405 scriptname = e_tmpname;
406 }
407</PRE>
408Pretty straightforward.<p>
409<LI> Build and install Perl.
410<PRE>
411> make
412<EM>[...]</EM>
413> make install
414</PRE>
415</UL>
416</P>
417<H4><a NAME = "S3.3">3.3 Advanced recipe</a></H4>
418
419<P>
420If you already have a Perl-5 package that has been configured, and you do
421not really want to re-run Configure, you can take the following steps.
422</P>
423<P ALIGN=CENTER><STRONG>THIS IS NOT RECOMMENDED</STRONG></P>
424<P>
425Edit config.sh with your favorite editor and modify the following lines:
426<DL>
427<DT><EM>cc</EM></DT>
428<DD>
429Change to use gcc if you are not using it already.
430</DD>
431<P></P>
432
433<DT><EM>ccflags</EM> AND <EM>cppflags</EM></DT>
434<DD>
435Add the following flags:
436<UL>
437<LI> <strong>-I<em>$fcgi</em>/include</strong>
438<LI> <strong>-include <em>$fcgi</em>/include/fcgi_stdio.h</strong>
439</UL>
440This assumes you are using GCC. See the above section on assumptions
441</DD>
442<P></P>
443
444<DT><EM>extensions</EM> AND <EM>known_extensions</EM></DT>
445<DD>
446Add FCGI to the list of extensions
447</DD>
448<P></P>
449
450<DT><EM>ldflags</EM></DT>
451<DD>
452Add -L $fcgi/libfcgi to the list.
453</DD>
454<P></P>
455
456<DT><EM>libs</EM></DT>
457<DD>
458Add -lfcgi to the list of libraries, should be added before -lc.
459</DD>
460<P></P>
461<DT><EM>static_ext</EM><STRONG> or </STRONG><EM>dynamic_ext</EM></DT>
462<DD>
463Add FCGI to the list of statically or dynamically loaded extensions.
464</DD>
465<P></P>
466<DT><EM>d_stdio_cnt_lval, d_stdio_ptr_lval, d_stdiobase, d_stdstdio</EM></DT>
467<DL>
468Change all of these to undef.
469</DL>
470<P>
471One you have edited config.sh you should do a "make Makefile depend all".
472If you're paranoid like me you may want to do a "make clean" first.
473</P>
474
475
476<H3><A NAME = "S4"> 4. Writing FastCGI applications in Perl</A></H3>
477<P>
478The Perl program <tt>examples/tiny-perl-fcgi</tt> performs the same function as
479the C program <tt>examples/tiny-fcgi</tt> that's used as an example in the
480<A HREF="fcgi-devel-kit.html#S3.1.1">FastCGI Developer's Kit document</A>.
481Here's what the Perl version looks like:
482</P>
483<pre>
484#!./perl
485use FCGI;
486$count = 0;
487while(FCGI::accept() >= 0) {
488 print("Content-type: text/html\r\n\r\n",
489 "&lt;title&gt;FastCGI Hello! (Perl)&lt;/title&gt;\n",
490 "&lt;h1&gt;FastCGI Hello! (Perl)&lt;/h1&gt;\n",
491 "Request number ", $++count,
492 " running on host &lt;i&gt;$ENV('SERVER_NAME')&lt;/i&gt;");
493}
494</pre>
495
496If you've built Perl according to the recipe and you have a Web server set
497up to run FastCGI applications, load the FastCGI Developer's Kit Index Page
498in that server and run this Perl application now.<p>
499
500The script invokes Perl indirectly via the symbolic link
501<tt>examples/perl</tt>. It does this because HP-UX has a limit of 32
502characters for the first line of a command-interpreter file such as
503<tt>examples/tiny-perl-fcgi</tt>. If you run on HP-UX you won't want
504to sprinkle symbolic links to perl everywhere, so you should
505choose a <tt>PERL_PREFIX</tt> shorter than <tt>/usr/local/perl5-fcgi</tt>.<p>
506
507You need to be aware of the following bug. If the
508initial environment to a FastCGI Perl application is empty (contains
509no name-value pairs) then when the first call to <tt>FCGI::accept</tt>
510returns, the environment will <i>still</i> be empty,
511i.e. <tt>%ENV</tt> will contain no associations. All the variables
512associated with the first request are lost. There are two known
513workarounds:<p>
514
515<ul>
516 <li>
517 In your Perl application, enumerate <tt>%ENV</tt> using
518 <tt>each</tt> before entering the <tt>FCGI::accept</tt>
519 loop. The program <tt>examples/tiny-perl-fcgi</tt>
520 contains code for this.<p>
521 <li>
522 In configuring your application be sure to set at least one
523 initial environment variable. You do this with the
524 <tt>AppClass -initial-env</tt> directive to the Web server,
525 or by running <tt>cgi-fcgi</tt> in a non-empty environment.
526</ul><p>
527
528The Perl subroutine <tt>FCGI::accept</tt> treats the initial
529environment differently than the C function <tt>FCGI_Accept</tt>. The
530first call to the
531C function <tt>FCGI_Accept</tt> replaces the initial environment with
532the environment of the first request. The first call to the Perl subroutine
533<tt>FCGI::accept</tt> adds the variable bindings of the first request
534to the bindings present in the initial environment. So when the first
535call to <tt>FCGI::accept</tt> returns, bindings from the initial
536environment are still there (unless, due to naming conflicts, some of
537them have been overwritten by the first request). The next call to
538<tt>FCGI::accept</tt> removes the bindings made on the previous call
539before adding a new set for the request just accepted, again preserving
540the initial environment.<p>
541
542The Perl <tt>FCGI</tt> module also includes
543subroutines <tt>FCGI::finish</tt>, <tt>FCGI::set_exit_status</tt>,
544and <tt>FCGI::start_filter_data</tt> that correspond to
545C functions in <tt>fcgi_stdio.h</tt>; see the manpages for
546full information.<p>
547
548Converting a Perl CGI application to FastCGI is not fundamentally
549different from converting a C CGI application. You separate
550the portion of the application that performs one-time
551initialization from the portion that performs per-request
552processing. You put the per-request processing into a loop
553controlled by <tt>FCGI::accept</tt>.<p>
554
555</body>
556</html>