views:

469

answers:

5

How can I write a web application in Perl so that it can work as plain CGI script, as FastCGI / FCGI script, and from mod_perl / mod_perl2 (preferably using only core Perl modules and those from libwww-Perl, and minimizing number of Perl CPAN modules one has to install)? I'd like to minimize changes one has to do to change script from using CGI to the one using FastCGI, or mod_perl.

Further constraint: if possible, I'd like to kee web application in single file, and not split it in modules, as it is currently.

The web app in question is gitweb, Git web interface, if it matters.

+2  A: 

Put the functionality in modules. Make the script perform just one function: Choose which application module to instantiate and take care of platform specific stuff and then delegate to the common functionality. So, you will end up with one script to invoke, three or four modules to implement each platform, and one module to implement general functionality.

App/bin/app.pl  

App/lib/App/Common.pm

App/lib/App/Apache1.pm
App/lib/App/Apache2.pm
App/lib/App/CGI.pm
App/lib/App/FCGI.pm
Sinan Ünür
+2  A: 

gitweb already uses CGI, which according to the Documentation (Disclaimer: I never tested this), supports all of those out of the box:

CGI.pm performs very well in in a vanilla CGI.pm environment and also comes with built-in support for mod_perl and mod_perl2 as well as FastCGI.

The last part is not exactly true, since you still need FCGI for FastCGI support, which needs a C compiler to install.

trendels
I was thinking about using more advanced features of mod_perl / mod_perl2, and actually using FastCGI (perhaps a FastCGI wrapper?)
Jakub Narębski
... And what would you do if you were running in CGI and these advanced features were not available? You're setting yourself up to write fast/elegant/whatever code for mod_perl and basic code for CGI, doubling your work...
ijw
+2  A: 

As Sinan points out, you separate the code into modules. He didn't use the magic term "Model-View-Controller" (MVC). You're really asking how to use MVC and support multiple controllers. Your model and views are the same, and your different controllers use them to drive the application.

brian d foy
+5  A: 

I'm surprised no one's given the obvious answer yet. Use Catalyst. With Catalyst, it's trivial to deploy in any web environment, including vanilla CGI, FastCGI, mod_perl, and more.

Dave Rolsky
A: 

Yet another possible solution would be to use HTTP::Engine
(see also Perl Programming/HTTP::Engine wikibook).

Jakub Narębski