Web::D...ethods(3pm)
π Web::Dispatch::HTTPMethods π
okay so bestie, this is Web::Dispatch::HTTPMethods β a little helper module from the User Contributed Perl Documentation crew that makes building RESTful dispatchers in Web::Simple way less of a whole thing. Like, it's giving "I got you, bestie" energy for real. π¦
β¨ NAME β¨
Web::Dispatch::HTTPMethods β Helpers to make RESTFul Dispatchers Easier. That's it, that's the name, and honestly? It ate. π
πΈ SYNOPSIS πΈ
Here's the vibe, copy-paste ready, no cap:
package MyApp:WithHTTPMethods;
use Web::Simple;
use Web::Dispatch::HTTPMethods;
sub as_text {
[200, ['Content-Type' => 'text/plain'],
[$_[0]->{REQUEST_METHOD}, $_[0]->{REQUEST_URI}] ]
}
sub dispatch_request {
sub (/get) {
GET { as_text(pop) }
},
sub (/get-head) {
GET { as_text(pop) }
HEAD { [204,[],[]] },
},
sub (/get-post-put) {
GET { as_text(pop) } ## NOTE: no commas separating http methods
POST { as_text(pop) }
PUT { as_text(pop) }
},
}
β heads up: in that last one, there are no commas between the http methods. That's not a typo, that's the whole point, iykyk.
π DESCRIPTION π
So basically this module exports the most commonly used HTTP methods as subroutine helpers straight into your Web::Simple based application. Bestie behaviour.
Using these methods also does some extra magic for you:
- it adds an automatic HTTP code
405"Method Not Allowed" response if none of the HTTP methods match for a given dispatch - it adds a dispatch rule for
HEADif noHEADexists but aGETdoes β in which case theHEADreturns theGETdispatch with an empty body
We also add support at the end of the chain for the OPTIONS method. This defaults to HTTP 200 OK + Allows http headers.
And we also try to set correct HTTP headers such as Allows as makes sense based on your dispatch chain. π
The following dispatch chains are basically the same:
sub dispatch_request {
sub (/get-http-methods) {
GET { [200, ['Content-Type' => 'text/plain'], ['Hello World']] }
},
sub(/get-classic) {
sub (GET) { [200, ['Content-Type' => 'text/plain'], ['Hello World']] },
sub (HEAD) { [200, ['Content-Type' => 'text/plain'], []] },
sub (OPTIONS) {
[200, ['Content-Type' => 'text/plain', Allows=>'GET,HEAD,OPTIONS'], []];
},
sub () {
[405, ['Content-Type' => 'text/plain', Allows=>'GET,HEAD,OPTIONS'],
['Method Not Allowed']]
},
}
}
The idea here is less boilerplate to distract the reader from the main point of the code, and also to encapsulate some best practices. Main character code, honestly. π
NOTE You currently cannot mix http method style and prototype sub style in the same scope, as in the following example:
sub dispatch_request {
sub (/get-head) {
GET { ... }
sub (HEAD) { ... }
},
}
If you try this, our code will notice and issue a die. π Not a vibe, but like, it's for your own good. If you have a good use case please bring it to the authors β they're listening, bestie.
π EXPORTS π
This automatically exports the following subroutines:
GET
PUT
POST
HEAD
DELETE
OPTIONS
six icons, all of them free, all of them yours β¨
π«Ά AUTHOR π«Ά
See Web::Simple for AUTHOR.
π§ CONTRIBUTORS π§
See Web::Simple for CONTRIBUTORS.
π COPYRIGHT π
See Web::Simple for COPYRIGHT.
π LICENSE π
See Web::Simple for LICENSE.
and that's the whole page, bestie β we ate and left no crumbs π β¨
this was perl v5.34.0, dated 2022-06-28, from the User Contributed Perl Documentation β ily, stay hydrated, and go write some Web::Simple dispatchers πππ¦
π¦ where this page comes from
- debian bookworm: libweb-simple-perl 0.033-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libweb-simple-perl 0.033-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libweb-simple-perl 0.033-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libweb-simple-perl 0.033-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libweb-simple-perl 0.033-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