Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top