Router::XS - Fast URI path to value lookup
use Router::XS ':all'; my $user_home_page = sub { ... }; add_route('/user/*', $user_home_page); my ($sub, @captures) = check_route('/user/foobar'); # or use HTTP method verbs to add routes get '/user/*' => sub { ... }; my ($sub, @captures) = check_route('GET/user/foobar');
Adds a new route associated with a subroutine to the router. Will die if a matching route has already been added. Accepts asterisks (*) as wildcards for captures. $path may be prepended with an HTTP method:
die
*
$path
add_route('POST/some/path', $sub);
Checks a URI path against the added routes and returns undef if no match is found, otherwise returning the associated subroutine reference and any captures from wildcards:
undef
my ($sub, @captures) = check_route('POST/some/path');
Sugar for add_route: adds a route using $path for the associated HTTP method:
add_route
put '/product/*' => sub { ... };
The any function accepts any HTTP method. When an incoming request is received, check_route must still be called.
any
check_route
See the test file included in this distribution for further examples.
Router::XS is not thread safe: however if you add all routes at the startup of an application under a single thread, and do not call add_route thereafter, it should be thread safe.
On my machine Router::XS performs well against other fast Routers. The test conditions add 200 routes, and then check how fast the router can match the path '/interstitial/track':
Rate Router::Boom Router::R3 Router::XS Router::Boom 344536/s -- -85% -93% Router::R3 2235343/s 549% -- -54% Router::XS 4860641/s 1311% 117% --
This module uses uthash to build a n-ary tree of paths. uthash is a single C header file, included in this distribution. uthash is copyright (c) 2003-2017, Troy D. Hanson.
uthash
perl Makefile.PL make make test make install
© 2017 David Farrell
The (two-clause) FreeBSD License
Thanks to ZipRecruiter for letting their employees contribute to Open Source.
There are many routers on CPAN including:
HTTP::Router
Path::Router
Router::Boom
Router::R3
To install Router::XS, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Router::XS
CPAN shell
perl -MCPAN -e shell install Router::XS
For more information on module installation, please visit the detailed CPAN module installation guide.