Router...okbook(3pm)
πΈ NAME πΈ
Okay so welcome to Router::Simple::Cookbook β aka The Router::Simple Cookbook. This page is Router...okbook(3pm) from the User Contributed Perl Documentation shelf, and yes the truncated title is part of the vibe, iykyk. Itβs basically a little recipe box of Router::Simple patterns, and we are going through every single one. π«Ά
π FAQ π
β¨ How to create Sinatra-ish framework with Router::Simple? β¨
Bestie, literally just read the following example code. Itβs giving tiny Sinatra-ish framework energy, and itβs all right here:
package MySinatraish;
use Router::Simple;
use Plack::Request;
sub import {
my $pkg = caller(0);
my $router = Router::Simple->new();
my $any = sub ($$;$) {
my ($pattern, $dest, $opt) = do {
if (@_ == 3) {
my ($methods, $pattern, $code) = @_;
($pattern, {code => $code}, +{method => [ map { uc $_ } @$methods ]});
} else {
my ($pattern, $code) = @_;
($pattern, {code => $code}, +{});
}
};
$router->connect(
$pattern,
$dest,
$opt,
);
};
no strict 'refs';
# any [qw/get post delete/] => '/bye' => sub { ... };
# any '/bye' => sub { ... };
*{"${pkg}::any"} = $any;
*{"${pkg}::get"} = sub {
$any->([qw/GET HEAD/], $_[0], $_[1]);
};
*{"${pkg}::post"} = sub {
$any->([qw/POST/], $_[0], $_[1]);
};
*{"${pkg}::as_psgi_app"} = sub {
return sub {
if (my $p = $router->match($_[0])) {
[200, [], [$p->{code}->()]];
} else {
[404, [], ['not found']];
}
}
};
}
package MyApp;
use MySinatraish;
get '/' => sub {
'top';
};
post '/new' => sub {
'posted';
};
as_psgi_app;
No skipping, okay? Every line is doing its little job. π
π¦ How to switch from HTTPx::Dispatcher? π¦
So HTTPx::Dispatcher is a class-specific declarative router β very structured, very βI have a type.β Hereβs the original flavor:
package MyApp::Dispatcher;
use HTTPx::Dispatcher;
connect '/', {controller => 'foo', action => 'bar'};
1;
And the following script is same as above. Same energy, new router bestie:
package MyApp::Dispatcher;
use Router::Simple::Declare;
my $router = router {
connect '/', {controller => 'foo', action => 'bar'};
};
sub match { $router->match() }
π How to use Router::Simple with non-strictly-MVC application? π
Hereβs the non-strictly-MVC setup, and itβs lowkey iconic:
use Router::Simple::Declare;
my $router = router {
connect '/foo/bar/' => { 'target' => '/foobar.asp' };
connect '/topics/:topic' => { target => '/my-topic.asp' };
connect '/products/{Category:.*}' => { target => '/products.asp', Category => 'All' };
connect '/zipcode/{zip:[0-9]{5,5}}' => {target => '/zipcode.asp' };
};
And yes, you can pass the target path as destination β thatβs the whole move. π
π AUTHOR π
Tokuhiro Matsuno <tokuhirom AAJKLFJEF GMAIL COM>
π LICENSE π
Copyright (C) Tokuhiro Matsuno
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
π SEE ALSO π
Router::Simple β go check it out, bestie. β¨
Cute little sign-off: this page says perl v5.42.0, dated 2015-04-13, and the footer is Router...okbook(3pm). Byeee π
π
π¦ where this page comes from
- opensuse-leap 16.0: perl-Router-Simple 0.17-bp160.1.12 Β· Artistic-1.0 or GPL-1.0+
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms