Web::Simple

A Perl Web Nano-Framework

Andrew “hobbs” Rodland

Another Framework?

PSGI Native

Example App

package MSTOMatic;
use Web::Simple; # Imports Moo and strictures
use JSON::XS 'encode_json';

has 'quotes' => (is => 'lazy');
sub _build_quotes { open my $fh, '<mstomatic.raw.txt'; [ <$fh> ] }

sub dispatch_request {
  my $self = shift;
  # ...
}
MSTOMatic->run_if_script;

Example App

has 'quotes' => (is => 'lazy');
sub _build_quotes { open my $fh, '<mstomatic.raw.txt'; [ <$fh> ] }

sub dispatch_request {
  my $self = shift;
  sub (GET + /mstomatic) {
    my @quotes = @{ $self->quotes };
    chomp(my $quote = $quotes[rand @quotes]);
    sub (.html) {
     return [200, ['Content-Type' => 'text/html'], [<<"EOHTML"]];
<html>
<head><title>The MST-O-MATIC</title></head>
<body><h1><a href="http://shadowcat.co.uk/blog/matt-s-trout">${quote}</a></h1>
</html>
EOHTML
    },
    sub (.json) {
      return [200, ['Content-Type' => 'application/json'], [encode_json({ quote => $quote })]];
    }
  }
}

Middleware?

sub dispatch_request {
  sub (GET + /mstomatic) {
    # ...
  }
}

Middleware

sub dispatch_request {
  sub (GET) {
    Plack::Middleware::Deflater->new;
  },
  sub (GET + /mstomatic) {
    # ...
  }
}

Plack App Dispatch

sub dispatch_request {
  sub (GET + /mstomatic) {
    # ...
  },
  sub (/browse/...) {
    Plack::App::Directory->new({ root => '.' })
  }
}

Works in Production

Other Features

Web::Dispatch

Community

IRC: irc.perl.org #web-simple

Demo