September 2023 Archives

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

I just discovered Dev.to

I don't really keep up with online resources, and I blogs.perl.org 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 dev.to because of a nice benchmarking article published by a long standing community member:

Benchmarking Perl Core Class in v5.38 By John Napiorkowski

https://dev.to/jjn1056/benchmarking-core-class-573c

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

About Brett Estrade

user-pic PAUSE Id: OODLER