项目作者: tokuhirom

项目描述 :
CGI::Emulate::PSGI
高级语言: Perl
项目地址: git://github.com/tokuhirom/p5-cgi-emulate-psgi.git
创建时间: 2009-10-13T06:13:07Z
项目社区:https://github.com/tokuhirom/p5-cgi-emulate-psgi

开源协议:Other

下载


NAME

CGI::Emulate::PSGI - PSGI adapter for CGI

SYNOPSIS

  1. my $app = CGI::Emulate::PSGI->handler(sub {
  2. # Existing CGI code
  3. });

DESCRIPTION

This module allows an application designed for the CGI environment to
run in a PSGI environment, and thus on any of the backends that PSGI
supports.

It works by translating the environment provided by the PSGI
specification to one expected by the CGI specification. Likewise, it
captures output as it would be prepared for the CGI standard, and
translates it to the format expected for the PSGI standard using
:PSGI">CGI::Parse::PSGI module.

CGI.pm

If your application uses CGI, be sure to cleanup the global
variables in the handler loop yourself, so:

  1. my $app = CGI::Emulate::PSGI->handler(sub {
  2. use CGI;
  3. CGI::initialize_globals();
  4. my $q = CGI->new;
  5. # ...
  6. });

Otherwise previous request variables will be reused in the new
requests.

Alternatively, you can install and use CGI::Compile from CPAN and
compiles your existing CGI scripts into a sub that is perfectly ready
to be converted to PSGI application using this module.

  1. my $sub = CGI::Compile->compile("/path/to/script.cgi");
  2. my $app = CGI::Emulate::PSGI->handler($sub);

This will take care of assigning a unique namespace for each script
etc. See CGI::Compile for details.

You can also consider using CGI::PSGI but that would require you to
slightly change your code from:

  1. my $q = CGI->new;
  2. # ...
  3. print $q->header, $output;

into:

  1. use CGI::PSGI;
  2. my $app = sub {
  3. my $env = shift;
  4. my $q = CGI::PSGI->new($env);
  5. # ...
  6. return [ $q->psgi_header, [ $output ] ];
  7. };

See CGI::PSGI for details.

METHODS

  • handler

    1. my $app = CGI::Emulate::PSGI->handler($code);

    Creates a PSGI application code reference out of CGI code reference.

  • emulate_environment

    1. my %env = CGI::Emulate::PSGI->emulate_environment($env);

    Creates an environment hash out of PSGI environment hash. If your code
    or framework just needs an environment variable emulation, use this
    method like:

    1. local %ENV = (%ENV, CGI::Emulate::PSGI->emulate_environment($env));
    2. # run your application

    If you use handler method to create a PSGI environment hash, this
    is automatically called in the created application.

AUTHOR

Tokuhiro Matsuno tokuhirom@cpan.org

Tatsuhiko Miyagawa

COPYRIGHT AND LICENSE

Copyright (c) 2009-2010 by tokuhirom.

This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the
LICENSE file included with this module.

SEE ALSO

PSGI CGI::Compile CGI::PSGI Plack :PSGI">CGI::Parse::PSGI