DEV Community

Dan
Dan

Posted on • Edited on

CGI::Tiny - Perl CGI, but modern

In a previous blog post, I explored the modern way to write CGI scripts using frameworks like Mojolicious. But as pointed out in comments, despite the many benefits, there is one critical problem: when you actually need to deploy to a regular CGI server, where the scripts will be loaded each time and not persisted, frameworks designed for persistent applications add lots of overhead to each request.

CGI scripts have historically been written using the CGI module (or even more ancient libraries). But this module is bulky, crufty, and has serious design issues that led to it being removed from Perl core.

Enter CGI::Tiny. It is built for one thing only: serving the CGI protocol. In most cases, frameworks are still the right answer, but in the case of scripts that are forced to run under the actual CGI protocol (such as shared web hosting), or when you want to just drop in CGI scripts with no need to scale, CGI::Tiny provides a modern alternative to CGI.pm. You can explore the interface differences from CGI.pm or suggested ways to extend CGI::Tiny scripts.

So without further ado, here is the equivalent CGI::Tiny script to my previous blog post's examples:

#!/usr/bin/env perl
use strict;
use warnings;
use CGI::Tiny;

cgi {
  my $cgi = $_;
  my $input = $cgi->param('input');
  $cgi->render(json => {output => uc $input});
};
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
yukikimoto profile image
Yuki Kimoto

Do you know light weight template modules with CGI::Tiny?

Collapse
 
grinnz profile image
Dan

see metacpan.org/pod/CGI::Tiny::Cookbo... for my suggestions. It depends what kind of lightweight you are looking for.

Collapse
 
yukikimoto profile image
Yuki Kimoto

Thanks. Mojo::Template can be used for this purpose!

Collapse
 
filbranden profile image
Filipe Brandenburger

Thanks for the trip down memory lane! 😁