Plack:...coding(3pm)
πΈ Plack::Request::WithEncoding β the manual page, bestie edition πΈ
okay so this is Plack:...coding(3pm), a User Contributed Perl Documentation page. yes the header is doing a little mystery with the truncation, we love that for it. anyway, sit down, hydrate, we're learning π
β¨ NAME β¨
Plack::Request::WithEncoding β a subclass of Plack::Request that supports encoded requests. it's giving "I speak every language, actually" π
π SYNOPSIS π
use Plack::Request::WithEncoding;
my $app_or_middleware = sub {
my $env = shift; # PSGI env
# Example of $env
#
# $env = {
# QUERY_STRING => 'query=%82%d9%82%b0', # <= encoded by 'cp932'
# REQUEST_METHOD => 'GET',
# HTTP_HOST => 'example.com',
# PATH_INFO => '/foo/bar',
# };
my $req = Plack::Request::WithEncoding->new($env);
$req->env->{'plack.request.withencoding.encoding'} = 'cp932'; # <= specify the encoding method.
my $query = $req->param('query'); # <= get parameters of 'query' that is decoded by 'cp932'.
my $res = $req->new_response(200); # new Plack::Response
$res->finalize;
};
look at that. $env gets built, the encoding gets set, the param comes back decoded. main character pipeline fr fr π₯
π¦ DESCRIPTION π¦
Plack::Request::WithEncoding is a subclass of Plack::Request that supports encoded requests. it overrides many Plack::Request attributes so they hand you back decoded values instead. the whole point? one single application can seamlessly handle a wide variety of different language code sets. no cap. apps that have to deal with tons of different translations all at once will find this extension able to quickly solve that problem. ate and left no crumbs π
the target attributes that get encoded are described down in "SPECIFICATION OF THE ENCODING METHOD". don't skip it, bestie, it's important.
π ATTRIBUTES of Plack::Request::WithEncoding π
encodingβ Returns an encoding method to decode parameters.query_parametersβ Returns a reference ofHash::MultiValueinstance that contains decoded query parameters.body_parametersβ Returns a reference ofHash::MultiValueinstance that contains decoded request body.parametersβ Returns a reference ofHash::MultiValueinstance that contains decoded parameters. The parameters are merged withquery_parametersandbody_parameters.paramβ Returns decoded parameters with a CGI.pm-compatible param method. This is an alternative method for accessing parameters in$req->parameters. Unlike CGI.pm, it does not allow setting or modifying query parameters.
$value = $req->param('foo');
@values = $req->param('foo');
@params = $req->param;
raw_query_parametersβ This attribute is the same asquery_parametersof Plack::Request.raw_body_parametersβ This attribute is the same asbody_parametersof Plack::Request.raw_parametersβ This attribute is the same asparametersof Plack::Request.raw_paramβ This attribute is the same asparamof Plack::Request.
so the raw_* squad is just the plain Plack::Request behaviour, untouched, unbothered, moisturised, in its lane π
β SPECIFICATION OF THE ENCODING METHOD β
You can specify the character-encoding to decode, like so;
$req->env->{'plack.request.withencoding.encoding'} = 'utf-7'; # <= set utf-7
When this character-encoding wasn't given through $req->env->{'plack.request.withencoding.encoding'}, this module uses utf-8 as the default character-encoding to decode. ngl it would be better to specify this character-encoding explicitly, because the readability and understandability of the code behavior would be improved. like, future-you will thank present-you π«Ά
Once this value was specified by falsy value (e.g. undef, 0 and ''), this module returns raw value (i.e. never decodes).
The example of a code is shown below.
print exists $req->env->{'plack.request.withencoding.encoding'} ? 'EXISTS'
: 'NOT EXISTS'; # <= NOT EXISTS
$query = $req->param('query'); # <= get parameters of 'query' that is decoded by 'utf-8' (*** YOU SHOULD NOT USE LIKE THIS ***)
$req->env->{'plack.request.withencoding.encoding'} = undef; # <= explicitly specify the `undef`
$query = $req->param('query'); # <= get parameters of 'query' that is not decoded (raw value)
$req->env->{'plack.request.withencoding.encoding'} = 'cp932'; # <= specify the 'cp932' as encoding method
$query = $req->param('query'); # <= get parameters of 'query' that is decoded by 'cp932'
β οΈ okay pay attention here bestie, the original is yelling at you in caps and honestly? valid. (*** YOU SHOULD NOT USE LIKE THIS ***) β do not rely on the default like that. be explicit. it's the difference between a clean vibe and a debugging spiral at 2am π
π₯ SEE ALSO π₯
Plack::Request
the origin story. the blueprint. go read it π
π LICENSE π
Copyright (C) moznion.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
π AUTHOR π
moznion <moznion@gmail.com>
and that's the whole page, no notes. this one's perl v5.30.3, dated 2020-08-08, brought to you by User Contributed Perl Documentation β stay decoded, stay iconic, mwah π
β¨π
π¦ where this page comes from
- debian bookworm: libplack-request-withencoding-perl 0.14-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libplack-request-withencoding-perl 0.14-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libplack-request-withencoding-perl 0.14-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libplack-request-withencoding-perl 0.14-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libplack-request-withencoding-perl 0.14-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libplack-request-withencoding-perl 0.14-1 Β· 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