UR::Se...Router(3pm)
π UR::Service::UrlRouter π
okay bestie, so this page lives in the User Contributed Perl Documentation section of the manual (it's a 3pm page, iykyk), and it's all about UR::Service::UrlRouter β a PSGI-aware router for incoming requests. yes. a router. for requests. it routes them. slay.
β¨ SYNOPSIS β¨
my $r = UR::Service::UrlRouter->create();
$r->GET('/index.html', \&handle_index);
$r->POST(qr(update/(.*?).html, \&handle_update);
my $s = UR::Service::WebServer->create();
$s->run( $r );
(and yes, that second line is exactly what it is. we do not touch it. we do not fix it. we simply observe it with love π«Ά)
πΈ DESCRIPTION πΈ
So basically this class is the middleman. the go-between. the group chat admin of your web app π It takes requests coming in from a PSGI server and routes each one to the right function that's supposed to handle it. no cap, that's the whole job. match request β hand it to handler.
π Properties
verboseβ ifverboseis true, the object will print the details of the handled requests toSTDOUT. loud and proud, honestly. it will narrate. π
π¦ Methods
$r->GET($URLish, $handler)
$r->POST($URLish, $handler)
$r->PUT($URLish, $handler)
$r->DELETE($URLisn, $handler)
These four β GET, POST, PUT, DELETE β are how you register a handler for a given request-method + URL pair. so: this method, this URL, that function. periodt.
The first argument is the URL to match against, and you can hand it over in one of these ways:
$stringβ a plain string matches the incoming request when the request's path iseqto that$string. simple. literal. iconic.qr(some regex (with) captures)β a regex matches the incoming request when the path matches the regex. and here's the fun part π if the regex has captures, those captures get passed as additional arguments to the$handler.$coderefβ a coderef matches the incoming request if$coderefreturns true.$coderefis given one argument: the PSGI env hashref.
and$handler? that's aCODEref. when it gets called, the first argument it receives is the standard PSGI env hashref. always. that's the vibe.
$r->__call__
__call__ is not meant to be called directly. like, don't. π
This class overloads the function dereference (call) operator so the object can be used as a callable object β as in $obj->(arg, arg). Because that's what overload expects, __call__ returns a code ref, and that code ref handles the PSGI request by finding an appropriate match between the incoming request and a handler you registered earlier. If nothing matches, it returns a 404 error code. bye π
And if multiple handlers match the incoming request? only the earliest registered handler gets called. first come, first served, no drama. π
π SEE ALSO π
UR::Service::WebServer, HTTP::Server::PSGI, Plack
that's the whole page, bestie!! β¨ this one's perl v5.32.1, dated 2022-01-17 β so go forth, route those requests, and remember: if nothing matches, it's a 404 and it's not your fault ππ
π¦ where this page comes from
- debian bookworm: libur-perl 0.470+ds-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libur-perl 0.470+ds-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libur-perl 0.470+ds-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms