WrapCGI: CGI.pm in your Catalyst app.

Catalyst::Controller::WrapCGI allows you to use CGI.pm directly in your Catalyst controller.

A huge benefit of this is quick porting of a legacy CGI.pm application directly into a Catalyst skeleton in an almost cut-and-paste fashion. You still have the full power of the Catalyst framework, but you don't lose any of your CGI application's functionality.

To demonstrate a quick WrapCGI application, we're going to write a pastebot using WrapCGI, Acme::Bleach (to keep our code clean :-) ) and for giggles, translate our application's output with Catalyst::Plugin::Acme::LOLCAT.

Packing list

Moose Don't freak out, this is simply to create an accessor :-)

Install, and we'll move on.

Getting Started

This article assumes you are familiar with creating a Catalyst application skeleton, so we're going to skip that part in this article.

Your first step (after creating your application) is to create a controller called Paste. You should also know how to do this, but for the sake of sanity, here's what it looks like:

    perl script/myapp_create.pl controller Paste

Next, open up this new controller in your favorite text editor and add the following lines:

    use Moose;
    use parent 'Catalyst::Controller::WrapCGI';

    use CGI qw/:html3/;

    has 'cgi' => (is =>'rw', isa =>'CGI', default => sub { CGI->new });

If you're not familiar with Moose at all, you should get to be :-). All this line is saying is "create an accessor called 'cgi' that when called with $self->cgi defaults to an anonymous sub that creates a new CGI object". Easy as pie :-)

Add the following lines to your controller after you've absorbed up to here:

 sub index :Path('/cgi-bin/paste.cgi')  {
     my ( $self, $c ) = @_;
     my $q = $self->cgi;
     $self->cgi_to_response( $c, sub {  

         print $q->header, $q->start_html('Clean your code!'),
               $q->h1('Paste your code here'),
                  -name   => 'paste',
                  -action => $c->uri_for('/cgi-bin/bleachit.cgi'),
               $q->textarea (
                 -name => 'code',
                 -rows => '10',
                 -cols => '80'
                 -name    => 'translate',
                 -value   => 'Paste it!',


 sub paste : Path("/cgi-bin/bleachit.cgi") {
     my ($self, $c) = @_;
     my $q = $self->cgi;

     $self->cgi_to_response( $c, sub {

         my $code = $q->param('code');
         my $cleaned_string = eval "use Acme::Bleach; $code" . "";

         print $q->header, $q->start_html('Cleaned code'),
         $q->h1("Your code, cleaned:"),


That's *it*. That's how WrapCGI integrates CGI into Catalyst.



Devin Austin aka dhoss