Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I port Perl code from mod_perl to FastCGI?

Tags:

We have an existing Perl application that supports mod_perl. However, our new host (Dreamhost) does not suppport mod_perl, only FastCGI; thus necessitating the port.

The existing code does not use any Apache specific stuff and is just normal Perl code written in way that's acceptable to mod_perl.

Reading the documentation and online tutorials, it appears that adding FastCGI support involves wrapping existing code inside a specific kind of loop. Below are the most commonly given skeleton code:

A. Using FCGI

use FCGI;
while (FCGI::accept >= 0)
{
    #Run existing code.
}

B. Using CGI::Fast

use CGI::Fast
while (my $cgi = CGI::Fast->new()) 
{  
   #Run existing code.
}

Sub-Questions:

  1. Are methods A and B equivalent ways to add FastCGI support?
  2. If A and B are different, what then are the pros and cons of using one over the other?
  3. Are there any best practices or gotchas one should know about when porting from mod_perl to FastCGI?

Thanks.

like image 765
GeneQ Avatar asked Mar 20 '09 04:03

GeneQ


2 Answers

Generically speaking, a FastCGI application is very similar to CGI. The major difference is that you can take advantage of the fact that your process is able to be persistent. You can leverage that to gain speed advantages in your application—for example, you can cache database data in your running process. Essentially, you're changing your application into its own application server, running behind a FastCGI gateway provided by the Web server.

The idea is to figure out how to make your application's means of processing applicable to a FastCGI gateway. Do you use any mod-perl specific functionality? If so, move away from that. If not, then just start working on talking via FastCGI. You've an advantage in that there are FastCGI interfaces available for Perl. I assume that you're using some sort of a version control system, so just make a branch that is for porting to FastCGI. Then, just start thinking about POST and PUT as reading from standard input and your application's responses as writing to standard output.

You may want to also just read through a library that implements a FastCGI interface for an application. You can find some of those at fastcgi.com. That might help for you to understand what your application is going to be doing differently in relation to what it is doing currently.

Good luck!

like image 52
Michael Trausch Avatar answered Sep 30 '22 16:09

Michael Trausch


From quickly looking at the CPAN docs, it looks like CGI::Fast is a wrapper around FCGI; from the CGI::Fast page:

In order to use CGI::Fast you'll need the FCGI module

My take is that it basically lets you use the standard functionality of CGI.pm with the speed benefits of FastCGI (header creation and parameter access being the main aspects of CGI.pm you are probably already using).

I haven't used either of these, this is just what it looks like to me from the documentation, so I could well be wrong.

like image 30
Cebjyre Avatar answered Sep 30 '22 16:09

Cebjyre