NAME

Path::Dispatcher - flexible and extensible dispatch

SYNOPSIS

use Path::Dispatcher;
my $dispatcher = Path::Dispatcher->new;

$dispatcher->add_rule(
    Path::Dispatcher::Rule::Regex->new(
        regex => qr{^/(foo)/},
        block => sub { warn $1; }, # foo
    )
);

$dispatcher->add_rule(
    Path::Dispatcher::Rule::Tokens->new(
        tokens    => ['ticket', 'delete', qr/^\d+$/],
        delimiter => '/',
        block     => sub { delete_ticket($3) },
    )
);

my $dispatch = $dispatcher->dispatch("/foo/bar");
die "404" unless $dispatch->has_matches;
$dispatch->run;

DESCRIPTION

We really like Jifty::Dispatcher and wanted to use it for Prophet's command line.

The basic operation is that of dispatch. Dispatch takes a path and a list of rules, and it returns a list of matches. From there you can "run" the rules that matched. These phases are distinct so that, if you need to, you can inspect which rules were matched without ever running their codeblocks.

You want to use Path::Dispatcher::Declarative which gives you some sugar inspired by Jifty::Dispatcher.

ATTRIBUTES

rules

A list of Path::Dispatcher::Rule objects.

name

A human-readable name; this will be used in the debugging hooks. In Path::Dispatcher::Declarative, this is the package name of the dispatcher.

METHODS

add_rule

Adds a Path::Dispatcher::Rule to the end of this dispatcher's rule set.

dispatch path -> dispatch

Takes a string (the path) and returns a Path::Dispatcher::Dispatch object representing a list of matches (Path::Dispatcher::Match objects).

run path, args

Dispatches on the path and then invokes the run method on the Path::Dispatcher::Dispatch object, for when you don't need to inspect the dispatch.

The args are passed down directly into each rule codeblock. No other args are given to the codeblock.

complete path -> strings

Given a path, consult each rule for possible completions for the path. This is intended for tab completion. You can use it with Term::ReadLine like so:

$term->Attribs->{completion_function} = sub {
    my ($last_word, $line, $start) = @_;
    my @matches = map { s/^.* //; $_ } $dispatcher->complete($line);
    return @matches;
};

This API is experimental and subject to change. In particular I think I want to return an object that resembles Path::Dispatcher::Dispatch.

AUTHOR

Shawn M Moore, <sartak at bestpractical.com>

BUGS

Please report any bugs or feature requests to bug-path-dispatcher at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Path-Dispatcher.

SEE ALSO

Jifty::Dispatcher
Catalyst::Dispatcher
HTTPx::Dispatcher
Mojolicious::Dispatcher
Path::Router
http://github.com/bestpractical/path-dispatcher-debugger - Not quite ready for release

COPYRIGHT & LICENSE

Copyright 2008-2009 Best Practical Solutions.

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