The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

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 the 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.

Most consumers would 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.

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

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.