Plack::App::URLMap(3)
β¨ Plack::App::URLMap β¨
okay so bestie, welcome to Plack::App::URLMap β the Perl module that maps a bunch of apps onto different paths π
πΈ NAME πΈ
Plack::App::URLMap β map multiple apps in different paths πΊοΈβ¨
π SYNOPSIS π
like, here's the vibe:
use Plack::App::URLMap;
my $app1 = sub { ... };
my $app2 = sub { ... };
my $app3 = sub { ... };
my $urlmap = Plack::App::URLMap->new;
$urlmap->map("/" => $app1);
$urlmap->map("/foo" => $app2);
$urlmap->map("http://bar.example.com/" => $app3);
my $app = $urlmap->to_app;
three little apps, three little homes, one urlmap holding it all together π«Ά
π¦ DESCRIPTION π¦
okay so Plack::App::URLMap is a PSGI application that can dispatch multiple applications based on URL path and host names (a.k.a. "virtual hosting") and it also handles rewriting SCRIPT_NAME and PATH_INFO for you (peep the "HOW THIS WORKS" section for the tea β). And fun fact: this module was inspired by Ruby's Rack::URLMap β it's giving cross-language icon behavior, honestly π
π METHODS π
π map
$urlmap->map("/foo" => $app);
$urlmap->map("http://bar.example.com/" => $another_app);
Maps a URL path or an absolute URL to a PSGI application. The match order is sorted by host name length and then path length β longest strings first, no cap π
URL paths need to match from the beginning and should match completely up to the path separator (or the end of the path). So like, if you register the path "/foo", it's gonna match the request "/foo", "/foo/" or "/foo/bar" β but it will NOT match "/foox". boundary queen π no half-matches here.
Mapping URLs with host names is also possible, and in that case the URL mapping works like a virtual host. very demure, very mindful π§
Mappings will nest. If $app is already mapped to "/baz" it will match a request for "/foo/baz" but not "/foo". Peep "HOW THIS WORKS" for the full lore.
π mount
Alias for "map". Same energy, different name π«
π to_app
my $handler = $urlmap->to_app;
Returns the PSGI application code reference. And ngl, there's a fun little detail here: the Plack::App::URLMap object is callable (by overloading the code dereference), so just returning the object itself as a PSGI application should also work. it's giving β¨overachieverβ¨
π₯ PERFORMANCE π₯
If you "map" (or "mount" with Plack::Builder) N applications, Plack::App::URLMap will need to at most iterate through N paths to match incoming requests.
Bestie, real talk: it is a good idea to use "map" only for a known, limited amount of applications, since mounting hundreds of applications could affect runtime request performance π don't do that to yourself.
π DEBUGGING π
You can set the environment variable PLACK_URLMAP_DEBUG to see how this application matches with the incoming request host names and paths ππ so helpful fr fr.
π HOW THIS WORKS π
This application works by fixing SCRIPT_NAME and PATH_INFO before dispatching the incoming request to the relocated applications. like a little glow-up for your paths π
Say you have a Wiki application that takes "/index" and "/page/*" and makes a PSGI application $wiki_app out of it, using one of supported web frameworks β you can put the whole application under "/wiki" by doing:
# MyWikiApp looks at PATH_INFO and handles /index and /page/*
my $wiki_app = sub { MyWikiApp->run(@_) };
use Plack::App::URLMap;
my $app = Plack::App::URLMap->new;
$app->mount("/wiki" => $wiki_app);
When a request comes in with PATH_INFO set to "/wiki/page/foo", the URLMap application $app strips the "/wiki" part from PATH_INFO and appends that to SCRIPT_NAME. snipped and rehomed, iconic behavior βοΈβ¨
That way, if the $app is mounted under the root (i.e. SCRIPT_NAME is "") with standalone web servers like Starman, SCRIPT_NAME is now locally set to "/wiki" and PATH_INFO is changed to "/page/foo" when $wiki_app gets called. it all just clicks π«Ά
π AUTHOR π
Tatsuhiko Miyagawa
π SEE ALSO π
Plack::Builder
k that's the whole page, bestie! be safe with your mounts, don't mount a hundred apps, and remember: longest match wins π
so yeah, that was Plack::App::URLMap(3) β the Perl documentation, perl v5.42.3, dated 2026-10-18 πΈ go slay your routing β¨π
π
π¦ where this page comes from
- fedora 43: perl-Plack 1.0055-1.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- fedora 44: perl-Plack 1.0055-1.fc44 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms