|
NAMEPath::Dispatcher - Flexible and extensible dispatchVERSIONversion 1.08SYNOPSISuse Path::Dispatcher; my $dispatcher = Path::Dispatcher->new; $dispatcher->add_rule( Path::Dispatcher::Rule::Regex->new( regex => qr{^/(foo)/}, block => sub { warn shift->pos(1); }, ) ); $dispatcher->add_rule( Path::Dispatcher::Rule::Tokens->new( tokens => ['ticket', 'delete', qr/^\d+$/], delimiter => '/', block => sub { delete_ticket(shift->pos(3)) }, ) ); my $dispatch = $dispatcher->dispatch("/foo/bar"); die "404" unless $dispatch->has_matches; $dispatch->run; DESCRIPTIONWe 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. Tab completion support is also available (see in particular "How can I configure tab completion for shells?" in Path::Dispatcher::Cookbook) for the dispatchers you write. Each rule may take a variety of different forms (which I think justifies the "flexible" adjective in the module's description). Some of the rule types are:
Since Path::Dispatcher is designed with good object-oriented programming practices, you can also write your own domain-specific rule classes (which earns it the "extensible" adjective). For example, in Prophet, we have a custom rule for matching, and tab completing, record IDs. You may want to use Path::Dispatcher::Declarative which gives you some sugar inspired by Jifty::Dispatcher. ATTRIBUTESrulesA list of Path::Dispatcher::Rule objects.METHODSadd_ruleAdds a Path::Dispatcher::Rule to the end of this dispatcher's rule set.dispatch path -> dispatchTakes a string (the path) and returns a Path::Dispatcher::Dispatch object representing a list of matches (Path::Dispatcher::Match objects).run path, argsDispatches 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 -> stringsGiven 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. SEE ALSO
SUPPORTBugs may be submitted through the RT bug tracker <https://rt.cpan.org/Public/Dist/Display.html?Name=Path-Dispatcher> (or bug-Path-Dispatcher@rt.cpan.org <mailto:bug-Path-Dispatcher@rt.cpan.org>).AUTHORShawn M Moore, "<sartak at bestpractical.com>"CONTRIBUTORS
COPYRIGHT AND LICENSEThis software is copyright (c) 2020 by Shawn M Moore.This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Visit the GSP FreeBSD Man Page Interface. |