1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
# PODNAME: Path::Dispatcher::Cookbook
# ABSTRACT: A cookbook for Path::Dispatcher
__END__
=pod
=encoding UTF-8
=head1 NAME
Path::Dispatcher::Cookbook - A cookbook for Path::Dispatcher
=head1 VERSION
version 1.08
=head1 NAME
Path::Dispatcher::Cookbook - A cookbook for Path::Dispatcher
=head1 RECIPES
=head2 How can I change the path delimiter from a space ' ' to a slash '/'?
When importing the L<Path::Dispatcher::Declarative> sugar, specify the
C<token_delimiter> option for the C<default> group.
package My::Dispatcher;
use Path::Dispatcher::Declarative -base, -default => {
token_delimiter => '/',
};
Or define a subclass of L<Path::Dispatcher::Declarative> with a
C<token_delimiter> method:
package Web::Dispatcher::Maker;
use base 'Path::Dispatcher::Declarative';
use constant token_delimiter => '/';
package My::Dispatcher;
use Web::Dispatcher::Maker -base;
=head2 How can I do rule chaining (like in Catalyst)?
You can use a C<chain> rule approximate chaining behavior:
package MyDispatcher;
use Path::Dispatcher::Declarative -base;
under show => sub {
chain {
print "Displaying ";
};
on inventory => sub {
print "inventory:\n";
...
};
on score => sub {
print "score:\n";
...
};
};
package main;
MyDispatcher->run("show inventory"); # "Displaying inventory:\n ..."
MyDispatcher->run("show score"); # "Displaying score:\n ..."
=head2 How can I configure tab completion for shells?
First add a dispatcher rule for generating completions based on the path. Here
we name it _gencomp, so that if the user types "app _gencomp hel" it will print
out the various completions of "hel".
on qr/^_gencomp\s*(.*)/ => sub {
my $prefix = shift->pos(1);
$prefix = "" if !defined($prefix);
print "$_\n" for dispatcher->complete($prefix);
};
Then tell your shell about how to use _gencomp. For zsh it might look
like this (replace "APP" with your binary name):
/usr/share/zsh/site-functions/_APP:
#compdef APP
typeset -a APP_completions
APP_completions=($($words[1] _gencomp $words[2,-1]))
compadd $APP_completions
For bash it might look like this:
/etc/bash_completion.d/APP.bash:
function _APP_()
{
COMPREPLY=($($1 _gencomp ${COMP_WORDS[COMP_CWORD]}))
}
complete -F _APP_ APP
=head1 SUPPORT
Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Path-Dispatcher>
(or L<bug-Path-Dispatcher@rt.cpan.org|mailto:bug-Path-Dispatcher@rt.cpan.org>).
=head1 AUTHOR
Shawn M Moore, C<< <sartak at bestpractical.com> >>
=head1 COPYRIGHT AND LICENSE
This 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.
=cut
|