CGI::E...::PSGI(3pm)

πŸ’– CGI::Emulate::PSGI β€” the manual, but make it cute πŸ’–

okay so bestie, this is the docs for CGI::Emulate::PSGI, a little Perl module that lives in the User Contributed Perl Documentation corner of your man pages. She's an icon, she's a legend, and she is literally the moment. πŸ¦‹


✨ NAME ✨

CGI::Emulate::PSGI β€” a PSGI adapter for CGI. she translates!! πŸ’…


🌸 SYNOPSIS 🌸

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

like... that's it. that's the whole vibe. your old CGI code goes in, a PSGI app comes out. πŸŽ€


πŸ¦‹ DESCRIPTION πŸ¦‹

So here's the tea β˜•: this module lets an application that was designed for the CGI environment run in a PSGI environment β€” which means it can run on any of the backends that PSGI supports. queen behavior, honestly.

How does it do it?? By translating, bestie. The environment that the PSGI spec hands you gets translated into the one the CGI spec expects. And on the flip side, it captures output the way it would be prepared for the CGI standard, then translates that into the format the PSGI standard expects β€” using the CGI::Parse::PSGI module to do it. πŸ”₯


πŸ“ CGI.pm πŸ“

okay this part matters, listen up πŸ“’

If your application uses CGI, you need to clean up the global variables in the handler loop yourself. like, do it yourself. so:

my $app = CGI::Emulate::PSGI->handler(sub {
    use CGI;
    CGI::initialize_globals();
    my $q = CGI->new;
    # ...
});

Otherwise previous request variables will be reused in the new requests. and bestie that is NOT the vibe. 😭 one request's leftovers showing up in another request?? embarrassing. clean up after yourself.


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

my $sub = CGI::Compile->compile("/path/to/script.cgi");
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. she handles the boring stuff so you don't have to. 🫢


You can also consider using CGI::PSGI, but heads up β€” that would require you to slightly change your code. from:

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

into:

use CGI::PSGI;

my $app = sub {
    my $env = shift;
    my $q = CGI::PSGI->new($env);
    # ...
    return [ $q->psgi_header, [ $output ] ];
};

See CGI::PSGI for details. different path, same destination, iykyk. πŸ’œ


πŸ’… METHODS πŸ’…

🌈 handler

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

Creates a PSGI application code reference out of a CGI code reference. code ref in, PSGI app out. transformative. ✨


πŸŽ€ emulate_environment

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

Creates an environment hash out of a PSGI environment hash. So if your code or your framework just needs an environment variable emulation and nothing else, use this method like:

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

And if you use the handler method to create a PSGI environment hash? this gets called automatically inside the created application. no extra work, she just does it. πŸ‘‘


πŸ‘‘ AUTHOR πŸ‘‘

  • Tokuhiro Matsuno <tokuhirom@cpan.org>
  • Tatsuhiko Miyagawa

two absolute legends fr fr. 🌟


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.

tl;dr β€” same terms as Perl itself, the receipts live in the LICENSE file. read it, bestie, it's giving responsible. 🧚

⭐ SEE ALSO ⭐

  • PSGI
  • CGI::Compile
  • CGI::PSGI
  • Plack
  • CGI::Parse::PSGI

and that's the whole page, periodt. πŸ’…

🌸 cute lil sign-off: this was perl v5.34.0, dated 2022-06-09, and the page you just read is CGI::E...::PSGI(3pm) β€” yes the truncated title is part of the branding, we love her anyway. mwah. πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from