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://

I just discovered

I don't really keep up with online resources, and I to be (like perl) a nice and stable home. I've watched resources come and go - I've lost untold content in the process (geocities, myspace, LtU, hello??).

I recently discovered because of a nice benchmarking article published by a long standing community member:

Benchmarking Perl Core Class in v5.38 By John Napiorkowski

So I guess this is like some kind of substack for developer blogging? I only recently discovered that resource also.

The Hidden Power of Prototypes


I have been leaning very heavily on Perl's prototype feature for creating new modules. The imptetus for this can traced back to the day I looked at the source code for Try::Tiny, and realized that it was implemented using prototypes. The main reason for using prototypes for many new modules I've created recently is my focus on making a thing I do repeatedly available in a more Perlish or idiomatic way.

Required reading if you have not and are about to dive into an article about prototypes: Far More Than Everything You've Ever Wanted to Know about Prototypes in Perl by Tom Christiansen.

The following article demonstrates 2 CPAN modules I have written that focus more on Perl programmer UX and why Perl prototypes can provide a way forward for a great many ideas that people have. Prototypes misunderstood, yes; but more so, they are misunderestimated. They are infact, very powerful and when used for their intended purpose; a lot of hand wringing can be avoided during feature discussions.

More prototype play: Dispatch::Fu

I was seeing this so much talk about smartmatch or given/when, I decided to experiment with a prototype powered pseudo structure I'd been thinking about. The results were pleasing to me, and I plan on releasing this soon. I could rename it to match/case (versus fu/on), but the huffman in me likes the short (and semi-ambiguous) "keywords". Let me know what you think in the comments below.

I still need to do more unit tests, POD, dist.ini, etc. But it's the closest I could get what I was observing on P5P. And the current implementation is about as fast and tight as I think it could be. I also enjoy using Perl's datatype coercion capabilities via prototypes* quite a bit. It is a very powerful, underutilized, and misunderstood capability that can be used to bring about a lot more ideas via a "keyword" experience (which is the entire point).


The first block computes a static key based on the contents of $bar. This is user defined, the dev gets a single reference to contain all the goods; they are then unpacked and a static key is computed. It's a form of reduction or digest, or to use a buzz word,classification. The key is then used to do an O(1) dispatch of the handler.

NOTE: it inverts and generalizes the application of the regexps in the "case" statements above. In Dispatch::Fu, a "case" (or on) is a static string; That string is determined in the "match" (or fu), so that's where you'd do the checking with regexps.

The following code works and is very fast. A more involved test from the snippet below can viewed here


use strict;
use warnings;
use Dispatch::Fu;    # exports 'fu' and 'on'

my $bar = [qw/1 2 3 4 5/];

fu {
    # here, give a reference $bar of any kind,
    # you compute a static string that is added
    # via the 'on' keyword; result will be
    # 'bucket' + some number in in 0-5

    my $baz = shift;
    return ( scalar @$baz > 5 )
      ? q{bucket5}
      : sprintf qq{bucket%d}, scalar @$baz;
  on bucket0 => sub { print qq{bucket 0\n} },
  on bucket1 => sub { print qq{bucket 1\n} },
  on bucket2 => sub { print qq{bucket 2\n} },
  on bucket3 => sub { print qq{bucket 3\n} },
  on bucket4 => sub { print qq{bucket 4\n} },
  on bucket5 => sub { print qq{bucket 5\n} };

Next step is to roll it into a release for CPAN. I will probably change the name, and for now am considering calling it a collapse (into static key) / on. Feedback welcome.


* Far More Than Everything You've Ever Wanted to Know about Prototypes in Perl.

Util::H2O and More, during Ordinary Times


During the 2022 Perl Advent, in particular the entry for December 06; Perl Advent fans were introduced to a little module called Util::H2O.

A lot has already been said about Util::H2O, and this author uses it a lot in client and production code because it helps produce very clean and maintainable HASH reference heavy code. So much so, that he created the Util::H2O::More module to encapsulate some common tasks and additional capabilities for working between pure Perl data structures and blessed objects that have real data accessors, in a natural and idiomatic way.

Support for Generic Perl Data Structures

h2o is perfect for dealing with data structures that are made up of strictly HASH references, but it is often the case that useful data structures contain a mix of HASH and ARRAY references. For example, when using databases or web API calls returning JSON, it is often a list of records that is returned. This was the case of the example call that was in the December 06 Perl Advent 2022 article.