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

Router::R3 - URL router library with high performance

SYNOPSIS

use Router::R3;

my $router = Router::R3->new(
  '/static/index.html' => 1,
  '/post/{id}' => 2,
  '/post_comment/{id:\d+}/{id2}' => 3,
);

my($match, $captures);
($match, $captures) = $router->match('/static/index.html'); # (1, {})
($match, $captures) = $router->match('/post/123'); # (2, { id => '123' })
($match, $captures) = $router->match('/post_comment/123/456'); # (3, { id => '123', id2 => '456' })
($match, $captures) = $router->match('/post_comment/xxx/456'); # ()  no match

# or you can pass a hashref or an arrayref when Router::R3->new

my $router = Router::R3->new(['/static', 1, '/post/{id}', 2]);
my $router = Router::R3->new({'/static' => 1, '/post/{id}' => 2});

# The latter one of each rule could be any perl scalar
#  It'll be given to you when the rule is matched.
#  It's better not to put anything which is treated as false here.

DESCRIPTION

This mod is a XS wrapper around a C library R3.

R3 is an URL router library with high performance, thus, it's implemented in C. It compiles your route paths into a prefix trie.

By using the constructed prefix trie in the start-up time, you can dispatch routes with efficiency.

PATTERN SYNTAX

/blog/post/{id}        use [^/]+ regular expression by default.
/blog/post/{id:\d+}    use `\d+` regular expression instead of default.
/blog/post/{id:\d{2}}  use `\d{2}` regular expression instead of default.

METHODS

$router = Router::R3->new(...)
The constructor
($matched, \%captures) = $router->match($test_string)
Match strings

SEE ALSO

The original C version "github repository" by c9s

This mod's "github repository" All the source files with this mod are in the Router-R3 directory.

AUTHOR

Cindy Wang (CindyLinz)

COPYRIGHT AND LICENSE

Copyright (C) 2014 by Cindy Wang (CindyLinz)

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8 or, at your option, any later version of Perl 5 you may have available.