Router::Dumb - yet another dumb path router for URLs
version 0.006
my $r = Router::Dumb->new; $r->add_route( Router::Dumb::Route->new({ parts => [ qw(group :group uid :uid) ], target => 'pants', constraints => { group => find_type_constraint('Int'), }, }), ); my $match = $r->route( '/group/123/uid/321' ); # $match->target returns 'pants' # $match->matches returns (group => 123, uid => 321)
Router::Dumb provides a pretty dumb router. You can add routes and then ask how to route a given path string.
Routes have a path. A path is an arrayref of names. Names that start with a colon are placeholders. Everything else is a literal. Literals pieces must appear, literally, in the string being routed. A placeholder can be satisfied by any value, as long as it satisfies the placeholder's constraint. If there's no constraint, any value works.
The special part * can be used to mean "...then capture everything else into the placeholder named REST."
*
REST
Most of the time, you won't be calling add_route, but using some other helper to figure out routes to add for you. Router::Dumb ships with Router::Dumb::Helper::FileMapper and Router::Dumb::Helper::RouteFile.
add_route
This library should run on perls released even a long time ago. It should work on any version of perl released in the last five years.
Although it may work on older versions of perl, no guarantee is made that the minimum required version will not be increased. The version may be increased for any reason, and there is no promise that patches will be accepted to lower the minimum required perl.
$router->add_route( Router::Dumb::Route->new({ parts => [ qw( the :path parts ) ], target => 'target-string', constraints => { path => $moose_tc, }, }) );
This method adds a new route to the router.
$router->add_route_unless_exists( Router::Dumb::Route->new({ parts => [ qw( the :path parts ) ], target => 'target-string', ... }) );
This method adds a new route to the router unless it would conflict, in which case it does nothing.
my $match_or_undef = $router->route( $str );
If the given string can be routed to a match, the match is returned. If not, the method returns false.
The string must begin with a /.
/
my @routes = $router->ordered_routes;
This method returns the router's routes, in the order that they will be checked. You probably do not want to use this method unless you really know what you're doing.
Ricardo Signes <cpan@semiotic.systems>
Karen Etheridge <ether@cpan.org>
Ricardo Signes <rjbs@semiotic.systems>
This software is copyright (c) 2022 by Ricardo Signes.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Router::Dumb, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Router::Dumb
CPAN shell
perl -MCPAN -e shell install Router::Dumb
For more information on module installation, please visit the detailed CPAN module installation guide.