CGI::Tiny & Dispatch::Fu - nearly a perfect match

CGI::Tiny is a very nice, Perlish way to construct CGI scripts these days. It is perfectly suited as a replacement to and is quite at home in a shared hosting environment.

Here's the example from the POD [1] on metacpan:

use CGI::Tiny;
cgi {
  my $cgi = $_;
  # set up error handling on $cgi
  # inspect request data via $cgi
  # set response headers if needed via $cgi
  # render response with $cgi->render or $cgi->render_chunk

I recently coupled this with my new module Dispatch::Fu [2], and it worked exceedingly well. It is a darn pleasure to consider from a distance, if I do say so myself:

use CGI::Tiny;
use Dispatch::Fu;

cgi {
  my $cgi = $_;
  dispatch {
    my $cgi = shift;
    if (defined $cgi->param('fribble')) {
      return 'fribble';
    return 'default';
  on fribble => sub { do_fribble($cgi) },
  on default => sub { do_default($cgi) }; #<~semicolon

sub fribble {
  my $cgi = shift;
  # application/json;charset=UTF-8
  $cgi->render(json         => { msg => 'fribble has left the building' });

sub default {
  # text/html;charset=$charset
  $cgi = $cgi->render(html  => '<html><head><title>default</title></head><body><h1>HI!!</h1></body></html>');

As you may be able to see, this code is able to scale very nicely as different display needs are added.





This article originally appeared at gopher://

Leave a comment

About Oodler 577

user-pic Mayor of Falvortown