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) }; #<~semicolon }; sub fribble { my $cgi = shift; # application/json;charset=UTF-8 $cgi->render(json => { msg => 'fribble says hi' }); } sub default { # text/html;charset=$charset $cgi = $cgi->render(html => '<html><head>...'); }
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
[3] Syntax highlighting done by converting Perl to HTML at https://tohtml.com/perl/
This article originally appeared at gopher://sdfeu.org/0/users/oodler577/phlogsrc/1694489548.19039.2023-09-12T03%3a32%3a28.txt
Leave a comment