Adapt cgi.pm to the psgi protocol
use CGI::PSGI; my $app = sub { my $env = shift; my $q = CGI::PSGI->new($env); return [ $q->psgi_header, [ $body ] ]; };
This module is for web application framework developers who currently uses \s-1CGI\s0 to handle query parameters, and would like for the frameworks to comply with the \s-1PSGI\s0 protocol.
Only slight modifications should be required if the framework is already collecting the body content to print to \s-1STDOUT\s0 at one place (rather using the print-as-you-go approach).
On the other hand, if you are an \*(L"end user\*(R" of \s-1CGI\s0.pm and have a \s-1CGI\s0 script that you want to run under \s-1PSGI\s0 web servers, this module might not be what you want. Take a look at CGI::Emulate::PSGI instead.
Your application, typically the web application framework adapter should update the code to do \*(C`CGI::PSGI->new($env)\*(C' instead of \*(C`CGI->new\*(C' to create a new \s-1CGI\s0 object. (This is similar to how CGI::Fast object is initialized in a FastCGI environment.)
Only the object-oriented interface of \s-1CGI\s0.pm is supported through \s-1CGI::PSGI\s0. This means you should always create an object with \*(C`CGI::PSGI->new($env)\*(C' and should call methods on the object.
The function-based interface like \*(C`use CGI ':standard'\*(C' does not work with this module.
\s-1CGI::PSGI\s0 adds the following extra methods to \s-1CGI\s0.pm:
$env = $cgi->env;
Returns the \s-1PSGI\s0 environment in a hash reference. This allows \s-1CGI\s0.pm-based application frameworks such as CGI::Application to access \s-1PSGI\s0 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 \*(C`env\*(C' method as well as the hash key \*(C`plack.session\*(C'.
my ($status_code, $headers_aref) = $cgi->psgi_header(%args);
Works like \s-1CGI\s0.pm's header(), but the return format is modified. It returns an array with the status code and arrayref of header pairs that \s-1PSGI\s0 requires.
If your application doesn't use \*(C`$cgi->header\*(C', you can ignore this method and generate the status code and headers arrayref another way.
my ($status_code, $headers_aref) = $cgi->psgi_redirect(%args);
Works like \s-1CGI\s0.pm's redirect(), but the return format is modified. It returns an array with the status code and arrayref of header pairs that \s-1PSGI\s0 requires.
If your application doesn't use \*(C`$cgi->redirect\*(C', you can ignore this method and generate the status code and headers arrayref another way.
Do not use CGI::Pretty or something similar in your controller. The module messes up \s-1CGI\s0's \s-1DIY\s0 autoloader and breaks \s-1CGI::PSGI\s0 (and potentially other) inheritance.
Tatsuhiko Miyagawa <[email protected]>
Mark Stosberg <[email protected]>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
\s-1CGI\s0, CGI::Emulate::PSGI