Paul Driver > HTTP-Server-Simple-Dispatched > HTTP::Server::Simple::Dispatched



Annotate this POD

View/Report Bugs
Module Version: 0.06   Source  


HTTP::Server::Simple::Dispatched - Django-like regex dispatching with request and response objects - no cruft!


Version 0.06


Quick and dirty regex-based dispatching inspired by Django, with standard response and request objects to ease some of the pain of HTTP::Server::Simple. Lower level than, which allows you to lose some of the cruft of that hoary old monster.

    use HTTP::Server::Simple::Dispatched qw(static);

    my $server = HTTP::Server::Simple::Dispatched->new(
      hostname => '',
      port     => 8081,
      debug    => 1,
      dispatch => [
        qr{^/hello/} => sub {
          my ($response) = @_;
          $response->content("Hello, world!");
          return 1;
        qr{^/say/(\w+)/} => sub {
          my ($response) = @_;
          $response->content("You asked me to say $1.");
          return 1;
        qr{^/counter/} => sub {
          my ($response, $request, $context) = @_;
          my $num = ++$context->{counter};
          $response->content("Called $num times.");
          return 1;
        qr{^/static/(.*\.(?:png|gif|jpg))} => static("t/"),
        qr{^/error/} => sub {
          die "This will cause a 500!";




The registry of mime types, this is a MIME::Types and is referenced during the serving of static files. Not exported by default.



Use this in dispatch specifiers to install a static-file handler. It takes one argument, a "root" directory. Your regex must capture the path from that root as $1 - e.g. "qr{^/some_path/(.*\.(?:png))} => static("foo/") to serve only .png files from foo/ as "/some_path/some.png". See the the 'static' example in the synopsis. Not exported by default.


These are Moose attributes: see its documentation for details, or treat them like regular perl read/write object accessors with convenient keyword arguments in the constructor.


An arrayref of regex object => coderef pairs. This bit is why you're using this module - you can map urls to handlers and capture pieces of the url. Any captures in the regex are bound to $1..$n just like in a normal regex. See the 'say' example in the synopsis. Note: these are matched in the order you specify them, so beware permissive regexes!


Not to be confused with the parent class's "host" accessor, the hostname has nothing to do with which interface to bind the server to. It is used to fill out Request objects with a full URI (in some cases, the locally known hostname for an interface is NOT what the outside world uses to reach it!


Every handler will get passed this object, which is global to the server. It can be anything, but defaults to a hashref. Use this as a quick and dirty stash, and then fix it with something real later.


If this is set to true, 500 errors will display some debugging information to the browser. Defaults to false.


If this is set true (which it is by default), requests for /some/method will be redirected to the /some/method/ handler (if such a handler exists). This is highly recommended, as many user agents start to append slashes if the last component of a path does not have an extension, and it makes things look a little nicer.



This is a proper subclass of HTTP::Server::Simple, but the constructor takes all ATTRIBUTES and standard PERLy accessors from the parent class as keyword arguments for convenience.


Paul Driver, <frodwith at>


Please report any bugs or feature requests to bug-http-server-simple-dispatched at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


The development branch lives at Creating your own branch and sending me the URL is the preferred way to send patches.


The static serve code was adapted from HTTP::Server::Simple::Static - I would have reused, but it didn't do what I wanted at all.

As mentioned elsewhere, Django's url dispatching is the inspiration for this module.


HTTP::Response, HTTP::Server::Simple::Dispatched::Request, MIME::Types, Moose, HTTP::Server::Simple::Dispatched


Copyright 2008 Paul Driver, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: