Tue Sep 12 03:32:28 UTC 2023
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 CGI.pm 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';
},
$cgi,
on fribble => sub { do_fribble($cgi) },
on default => sub { do_default($cgi) }; #<~ last must end with a semi-colon
};
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.
Enjoy!
References:
[1]
https://metacpan.org/pod/CGI::Tiny
[2]
https://metacpan.org/pod/Dispatch::Fu
This article now appears online the big bloated WWW at:
https://blogs.perl.org/users/oodler_577/2023/09/cgitiny-dispatchfu---nearly-a-perfect-match.html