[dancer-users] CGI must DIE! .... or Dancer::Plugin::FakeCGI

igor.bujna at post.cz igor.bujna at post.cz
Tue Feb 26 11:45:37 GMT 2013


Hi,
first i want thanks to Sawyer X about his good presentation on YAPC about 
CGI must DIE! I find only this video http://www.youtube.com/watch?v=tu6_3
fZbWYw.

I have couple of project which use CGI.pm. I thinking about how can i simply
rewrite this to any MVC framework. Couple of years ago, ther isn;t exist any
usable MVC framework and every of us use CGI or other similar functions for 
creating HTML pages. How SawyerX said, CGI must die. Because working on big 
project writed on CGI, than one time you will have to much problem to keep 
consistence of programs.

Before couple years ago i must writed one CRM project in Perl and try to 
find which frameworkt can use for this. I play with CGI::Application, 
Gantry, Jifty, Mojo, Catalyst. Winner of this was Catalyst, but next i 
finded newer project - Dancer :) and try to made this project atop Dancer. 
For me Dancer was and for now is winner as Perl web framework. Because is 
small, simpliest and made everything what i need. Catalyst is powerfull but 
bigger. 

Now when i thinking how move my CGI projects to Dancer i finded couple of 
solutions(
Catalyst::Controller::CGIBin, HTTP::Request::AsCGI, CGI::PSGI, CGI::
Emulate::PSGI)
  but none of this make everything what i need. Than i make Dancer::Plugin::
FakeCGI. http://search.cpan.org/~koceasy/Dancer-Plugin-FakeCGI-0.60/lib/
Dancer/Plugin/FakeCGI.pm

This plugin made emulation of Mod_PERL for now in version 1. I choose this, 
because any of us which use CGI.pm we had to use a mod_perl funcionalities 
to make better programs runned atop CGI.pm. You look on examples directories
and run bin/app.pl from this.

When you have bigger project writed atop CGI.pm than you have big problem to
rewriting this to anythink else. Rewriting use yours time, which can you 
spend to extended funcionalities. It can look like this:

Web--->INPUT-->CGI project do something--->HTML contentt

I try to emulated mod_perl behavior in Dancer::Plugin::FakeCGI. First 
emulated input parametters under mod-Perl  and second is capture everything 
what CGI project print to STDOUT. If i hade this than can be simple make any
changes or funcionalities in this CGI. For this i use mocking and this 
package http://search.cpan.org/~sukria/Test-TinyMocker-0.03/lib/Test/
TinyMocker.pm(I send simply patch to Alexis Sukrieh to extend his package to
method mocked, which can return original mocked method, but he dosn't answer
on my email)(http://search.cpan.org/%7Esukria/). Now if i have this 
functionalities, than i have simplies change fincionalities in CGI project. 
I can mocked any function and if CGI project run this function than i return
what i need, simply example is /test_6 under examples directories or this 
example:

sub test_file { 
   my ($cgi_bin, $cgi, $url, $is_perl) = @_;
   return 1 if ($cgi =~ /^\./); # skip serving this file https://email.
seznam.cz/#compose/30576
   return 1 if ($cgi =~ /test.pl/); # skip serving this file 
   if ($cgi eq "index.pl") { # own serving for given file 
       any $url => sub { redirect "/index.sh"; };
       return 1;
   } 
   return 0; # default serving file
 }

fake_cgi_bin(\&test_file, ["*.pl", "*.sh"], 1);  
# Read every files *.sh or *.pl and try to serve automaticaly if method test
_file() return 0;

There is couple of problem when use this plugin:
1) in CGI can't be any infinity loop such us CGI::Push.pm or other, because 
i use capturing STDOUT and after CGI program exited than i server this 
captured informations-HTML pages to client
2) Problem with fork|system() function under HTTP::Server::Simple - This 
server make bad openening file descriptor of STDOUT on child pid and open 
STDOUT as STDERR of parent pid. Solution for this is use plackup
3) better solution for capturing STDOUT under system call when is set 
capture to memory.
4) Don't use dancer under mod_perl in apache. because I emulated mod_perl 
funcionalities. Solution for this is use Apache as proxy and serving 
connection to plackup server.
5) Serving file is about 20-50% slowest than under Mod_perl when use 
Registry.pm, because CGI::Compile behaviour is as PerlRun.pm. Now i worked 
on similar behavior as Registry.pm.
6) memory usage us bigger than on mod_perl - solution for this is buy more 
memory :D 

My reasons is this will be good for simple migration from CGI to Dancer. Now
you can simply run any CGI project under Dancer and make simple refactoring 
of this code.
 
Bye Igor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20130226/b1db2c1e/attachment.htm>


More information about the dancer-users mailing list