Science and technology

An introduction to the Web::Simple Perl module, a minimalist net framework

One of the more-prominent members of the Perl group is Matt Trout, technical director at Shadowcat Systems. He’s been constructing core instruments for Perl functions for years, together with being a co-maintaner of the Catalyst MVC (Model, View, Controller) net framework, creator of the DBIx::Class object-management system, and far extra. In particular person, he is energetic, fascinating, good, and typically arduous to maintain up with. When Matt writes code…effectively, consider a runaway chainsaw, with the set off taped down and the security options disabled. He’s off and working, and also you by no means fairly know what’s going to come out. Two issues are virtually sure: the module will exactly match the aim Matthew has in thoughts, and it’ll present up on CPAN for others to make use of.

One of Matt’s special-purpose modules is Web::Simple. Touted as “a quick and easy way to build simple web applications,” it’s a stripped-down, minimalist net framework, with a straightforward to be taught interface. Web::Simple is in no way designed for a large-scale utility; nevertheless, it could be ideally suited for a small software that does one or two issues in a lower-traffic atmosphere. I also can envision it getting used for speedy prototyping should you needed to create fast wireframes of a brand new utility for demonstrations.

Installation, and a fast “Howdy!”

You can set up the module utilizing cpan or cpanm. Once you’ve got bought it put in, you are prepared to write down easy net apps with out having to problem with managing the connections or any of that—simply your performance. Here’s a fast instance:

#!/usr/bin/perl
package HelloReader;
use Web::Simple;

sub dispatch_request
  GET => sub
    [ 200, [ 'Content-type', 'textual content/plain' ], [ 'Howdy, Opensource.com reader!' ] ]
  ,
  '' => sub

HelloReader->run_if_script;

There are a few issues to note proper off. For one, I did not use strict and use warnings like I normally would. Web::Simple imports these for you, so you do not have to. It additionally imports Moo, a minimalist OO framework, so if Moo and wish to use it right here, you possibly can! The coronary heart of the system lies within the dispatch_request technique, which you need to outline in your utility. Each entry within the technique is a match string, adopted by a subroutine to reply if that string matches. The subroutine should return an array reference containing standing, headers, and content material of the reply to the request.

Matching

The matching system in Web::Simple is highly effective, permitting for classy matches, passing parameters in a URL, question parameters, and extension matches, in just about any mixture you need. As you possibly can see within the instance above, beginning with a capital letter will match on the request technique, and you’ll mix that with a path match simply:

'GET + /particular person/*' = sub ,
'POST + /particular person/* + %*' = sub
  my ($self, $particular person, $params) = @_;
  # write some code to switch an individual, maybe
 

In the latter case, the third a part of the match signifies that we should always choose up all of the POST parameters and put them in a hashref referred to as $params to be used by the subroutine. Using ? as a substitute of % in that a part of the match would choose up question parameters, as usually utilized in a GET request. There’s additionally a helpful exported subroutine referred to as redispatch_to. This software permits you to redirect, with out utilizing a 3xx redirect; it is dealt with internally, invisible to the consumer. So:

'GET + /some/url' = sub

A GET request to /some/url would get dealt with as if it was despatched to /some/different/url, with out a redirect, and the consumer will not see a redirect of their browser.

I’ve simply scratched the floor with this module. If you are in search of one thing production-ready for bigger tasks, you may be higher off with Dancer or Catalyst. But with its mild weight and built-in Moo integration, Web::Simple packs a sufficiently big punch for quite a lot of one-offs and smaller providers.

Most Popular

To Top