Perl Advent Calendar 2006-12-04

Are you naughty or nice?

by Jerrad Pierce

Like templating systems, many have tried to implement their own CGI query processor. But the instant any such hack were to see the light of day, it'd meet with cries of, "use CGI!" Often, besides ignorance, these homegrown solutions stem from a desire to avoid the heft of CGI. What if you don't need all of its bells and whistles? Perhaps you've decided to try to get off of a certain someone's Naughty list and use something like HTML::Template, rendering all of CGI's HTML generator methods dead wood. Well then, you might just be interested in the svelte CGI::Minimal. Depending upon how you measure it Minimal is about 1/6 the lines and 1/9 bytes of CGI. The result, according to the (dated) benchmarks included in the documentation, is a 4x speedup for conventional CGIs. And as our simple sample cgiecho shows 1, the module's interface is familiar enough. So go ahead and check it out, what've you got to lose?

   1 #Be sure to use -T and maybe CGI::Untaint if you do anything more substantial
   2 use CGI::Minimal;
   3 use Data::Dumper;
   5 my($q, %p);
   7 $q = new CGI::Minimal;
   8 $p{$_} = $q->param($_) foreach $q->param();
  10 print "Content-type: text/plain\n\n";
  12 $Data::Dumper::Sortkeys =1 ;
  13 print Data::Dumper::Dumper \%p;


CGI::Simple for a middle ground between CGI and ::Minimal.


1. No templating system is used as it was felt doing so would be overkill and beyond the scope of the example.