| 1 |
NAME |
| 2 |
|
| 3 |
CGI::Emulate::PSGI - PSGI adapter for CGI |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
|
| 7 |
my $app = CGI::Emulate::PSGI->handler(sub { |
| 8 |
# Existing CGI code |
| 9 |
}); |
| 10 |
|
| 11 |
DESCRIPTION |
| 12 |
|
| 13 |
This module allows an application designed for the CGI environment to |
| 14 |
run in a PSGI environment, and thus on any of the backends that PSGI |
| 15 |
supports. |
| 16 |
|
| 17 |
It works by translating the environment provided by the PSGI |
| 18 |
specification to one expected by the CGI specification. Likewise, it |
| 19 |
captures output as it would be prepared for the CGI standard, and |
| 20 |
translates it to the format expected for the PSGI standard using |
| 21 |
CGI::Parse::PSGI module. |
| 22 |
|
| 23 |
CGI.pm |
| 24 |
|
| 25 |
If your application uses CGI, be sure to cleanup the global variables |
| 26 |
in the handler loop yourself, so: |
| 27 |
|
| 28 |
my $app = CGI::Emulate::PSGI->handler(sub { |
| 29 |
use CGI; |
| 30 |
CGI::initialize_globals(); |
| 31 |
my $q = CGI->new; |
| 32 |
# ... |
| 33 |
}); |
| 34 |
|
| 35 |
Otherwise previous request variables will be reused in the new |
| 36 |
requests. |
| 37 |
|
| 38 |
Alternatively, you can install and use CGI::Compile from CPAN and |
| 39 |
compiles your existing CGI scripts into a sub that is perfectly ready |
| 40 |
to be converted to PSGI application using this module. |
| 41 |
|
| 42 |
my $sub = CGI::Compile->compile("/path/to/script.cgi"); |
| 43 |
my $app = CGI::Emulate::PSGI->handler($sub); |
| 44 |
|
| 45 |
This will take care of assigning a unique namespace for each script |
| 46 |
etc. See CGI::Compile for details. |
| 47 |
|
| 48 |
You can also consider using CGI::PSGI but that would require you to |
| 49 |
slightly change your code from: |
| 50 |
|
| 51 |
my $q = CGI->new; |
| 52 |
# ... |
| 53 |
print $q->header, $output; |
| 54 |
|
| 55 |
into: |
| 56 |
|
| 57 |
use CGI::PSGI; |
| 58 |
|
| 59 |
my $app = sub { |
| 60 |
my $env = shift; |
| 61 |
my $q = CGI::PSGI->new($env); |
| 62 |
# ... |
| 63 |
return [ $q->psgi_header, [ $output ] ]; |
| 64 |
}; |
| 65 |
|
| 66 |
See CGI::PSGI for details. |
| 67 |
|
| 68 |
METHODS |
| 69 |
|
| 70 |
handler |
| 71 |
|
| 72 |
my $app = CGI::Emulate::PSGI->handler($code); |
| 73 |
|
| 74 |
Creates a PSGI application code reference out of CGI code reference. |
| 75 |
|
| 76 |
emulate_environment |
| 77 |
|
| 78 |
my %env = CGI::Emulate::PSGI->emulate_environment($env); |
| 79 |
|
| 80 |
Creates an environment hash out of PSGI environment hash. If your |
| 81 |
code or framework just needs an environment variable emulation, use |
| 82 |
this method like: |
| 83 |
|
| 84 |
local %ENV = (%ENV, CGI::Emulate::PSGI->emulate_environment($env)); |
| 85 |
# run your application |
| 86 |
|
| 87 |
If you use handler method to create a PSGI environment hash, this is |
| 88 |
automatically called in the created application. |
| 89 |
|
| 90 |
AUTHOR |
| 91 |
|
| 92 |
Tokuhiro Matsuno <[email protected]> |
| 93 |
|
| 94 |
Tatsuhiko Miyagawa |
| 95 |
|
| 96 |
COPYRIGHT AND LICENSE |
| 97 |
|
| 98 |
Copyright (c) 2009-2010 by tokuhirom. |
| 99 |
|
| 100 |
This program is free software; you can redistribute it and/or modify it |
| 101 |
under the same terms as Perl itself. |
| 102 |
|
| 103 |
The full text of the license can be found in the LICENSE file included |
| 104 |
with this module. |
| 105 |
|
| 106 |
SEE ALSO |
| 107 |
|
| 108 |
PSGI CGI::Compile CGI::PSGI Plack CGI::Parse::PSGI |
| 109 |
|