[Catalyst-commits] r8918 - trunk/examples/CatalystAdvent/root/2008/pen

dhoss at dev.catalyst.perl.org dhoss at dev.catalyst.perl.org
Thu Dec 18 21:14:31 GMT 2008


Author: dhoss
Date: 2008-12-18 21:14:31 +0000 (Thu, 18 Dec 2008)
New Revision: 8918

Modified:
   trunk/examples/CatalystAdvent/root/2008/pen/WrapCGI.pod
Log:
Un-truncated! Finished, plz review.


Modified: trunk/examples/CatalystAdvent/root/2008/pen/WrapCGI.pod
===================================================================
--- trunk/examples/CatalystAdvent/root/2008/pen/WrapCGI.pod	2008-12-18 21:07:24 UTC (rev 8917)
+++ trunk/examples/CatalystAdvent/root/2008/pen/WrapCGI.pod	2008-12-18 21:14:31 UTC (rev 8918)
@@ -2,4 +2,97 @@
 
 L<Catalyst::Controller::WrapCGI> allows you to use CGI.pm directly in your Catalyst controller.
 
-A huge benefit of this is quick porting of a 
+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 B<still> have the full power of the Catalyst framework and 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.
+
+=head2 Packing list
+
+=over 12
+
+=item C<Catalyst::Controller::WrapCGI>
+
+=item C<Acme::Bleach>
+
+=item C<Catalyst::Plugin::Acme::LOLCAT>
+
+=item C<Moose> Don't freak out, this is simply to create an accessor :-)
+
+=back
+
+Install, and we'll move on.
+
+=head2 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'),
+              $q->start_form(
+                 -name   => 'paste',
+                 -action => $c->uri_for('/cgi-bin/bleachit.cgi'),              
+              ),
+              $q->textarea (
+                -name => 'code',
+                -rows => '10',
+                -cols => '80'
+              ),
+              $q->br,
+              $q->submit(
+                -name    => 'translate',
+                -value   => 'Paste it!',
+              ),
+              $q->end_form,
+              $q->end_html;
+    });
+
+}
+
+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" . "";
+        $c->log->debug("Code: $code");
+        $c->log->debug("Clean code: $cleaned_string");
+ 
+        print $q->header, $q->start_html('Cleaned code'),
+        $q->h1("Your code, cleaned:"),
+        $q->pre($cleaned_string),
+        $q->end_html;
+    });
+
+}
+
+
+That's *it*.  That's how WrapCGI integrates CGI into Catalyst.  
+
+Tada!




More information about the Catalyst-commits mailing list