CGI::PSGI(3pm)
π CGI::PSGI β the manual page, but make it make sense β¨
okay so bestie, this is CGI::PSGI, living in the CGI::PSGI(3pm) manual, and it's literally the little adapter that lets your old-school CGI.pm code vibe with the PSGI protocol. no cap. π¦
πΈ SYNOPSIS πΈ
use CGI::PSGI;
my $app = sub {
my $env = shift;
my $q = CGI::PSGI->new($env);
return [ $q->psgi_header, [ $body ] ];
};
π DESCRIPTION π
This module is for web application framework developers β the ones who are already using CGI to handle query parameters and are like "hiii I would love for my framework to comply with the PSGI protocol" π«Ά
The good news? Only slight modifications should be required β but only if your framework is already collecting the body content to print to STDOUT in one place, instead of doing the print-as-you-go thing. if you're printing as you go... bestie, we gotta talk. π
On the other hand π β if you're an "end user" of CGI.pm and you've got a CGI script you just want to run under PSGI web servers, this module might not be what you want. go look at CGI::Emulate::PSGI instead. it's giving "different tool for a different job." π
Your application β typically the web application framework adapter β should update the code to do CGI::PSGI->new($env) instead of CGI->new to create a new CGI object. (This is similar to how a CGI::Fast object is initialized in a FastCGI environment, iykyk.)
β¨ INTERFACES SUPPORTED β¨
Only the object-oriented interface of CGI.pm is supported through CGI::PSGI. Which means: you should always create an object with CGI::PSGI->new($env) and call methods on the object. that's the whole vibe. π
The function-based interface β like use CGI ':standard' β does not work with this module. not me finding that out the hard way π
π METHODS π
CGI::PSGI adds the following extra methods to CGI.pm:
π env
$env = $cgi->env;
Returns the PSGI environment in a hash reference. This is the slay part β¨ because it lets CGI.pm-based application frameworks β such as CGI::Application β access PSGI extensions, typically set by Plack Middleware components.
So if you enable Plack::Middleware::Session, your application and plugin developers can access the session via:
$cgi->env->{'plack.session'}->get("foo");
Of course, this should be coded carefully by checking the existence of the env method as well as the hash key plack.session. π§ don't just rawdog it, bestie.
π psgi_header
my ($status_code, $headers_aref) = $cgi->psgi_header(%args);
Works like CGI.pm's header(), but the return format is modified. It returns an array with the status code and an arrayref of header pairs that PSGI requires. iconic behavior honestly.
If your application doesn't use $cgi->header, you can ignore this method and generate the status code and headers arrayref another way. no pressure. π
π psgi_redirect
my ($status_code, $headers_aref) = $cgi->psgi_redirect(%args);
Works like CGI.pm's redirect(), but the return format is modified. It returns an array with the status code and an arrayref of header pairs that PSGI requires. same energy as above, just the redirect flavor. π₯
If your application doesn't use $cgi->redirect, you can ignore this method and generate the status code and headers arrayref another way.
π LIMITATIONS π
Do not use CGI::Pretty or something similar in your controller. The module messes up CGI's DIY autoloader and breaks CGI::PSGI (and potentially other) inheritance. this is not a drill, bestie. it will break things. π
π AUTHOR π
Tatsuhiko Miyagawa <miyagawa@bulknews.net>
Mark Stosberg <mark@summersault.com>
β LICENSE β
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. slay, open source. π
π SEE ALSO π
CGICGI::Emulate::PSGI
okay that's the whole page π«Ά this was written for perl v5.34.0, dated 2022-06-10, and it's the CGI::PSGI(3pm) manual β go forth and adapt your CGI to PSGI, bestie, you got this π
β¨
π¦ where this page comes from
- debian bookworm: libcgi-psgi-perl 0.15-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libcgi-psgi-perl 0.15-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libcgi-psgi-perl 0.15-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libcgi-psgi-perl 0.15-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libcgi-psgi-perl 0.15-3 Β· 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