in reply to Re: Change cgi script to PSGI app
in thread Change cgi script to PSGI app

Toby, just keep writing more of those “ramblings” of yours, anytime!   But I do find myself with a couple of questions / points for clarification, if you would, please.

Ordinarily, I do use PSGI, by means of Plack.   (N-O-T to ignite any religious wars here, I do not use mod_perl.)   If everything is going to be done on a single computer, I simply let Apache own the child processes, e.g. using mod_fastcgi.   My only use for plackup is as a command-line tool which allows me to run the scripts and connect to them on localhost for development purposes only; it is never put into any “shebang” lines.   Therefore, I am quite puzzled by your other suggested use for it.   I don’t quite understand you on that point ...

Although I have never had need to use Plack’s adapter for CGI-scripts, its docs seem sensible.   Quite frankly, I just design apps to run under Plack.

Replies are listed 'Best First'.
Re^3: Change cgi script to PSGI app
by tobyink (Canon) on Sep 12, 2014 at 09:00 UTC

    "Therefore, I am quite puzzled by your other suggested use for it. I don’t quite understand you on that point"

    The use of plackup in a shebang line for CGI scripts is documented in Plack::Handler::CGI. If you're interested in how/why it works, read on.

    plackup's normal behaviour is to run a server which listens on a TCP port, and handles multiple HTTP requests, and keeps going until it gets killed. You can observe this behaviour by running:

    plackup -e'sub { [200,[],[q/ok/]] }'

    It will print out a URL; you can paste that into your browser, and it will serve up a small text file containing the word "ok". And you can hit "reload" and it will do it again and again. Awesome. But, of course, not what you want from a CGI script! So how is plackup any good in a shebang line?

    Hit Ctrl+C to kill plackup, and then set the following environment variables:

    GATEWAY_INTERFACE=1 SERVER_NAME=localhost SERVER_PORT=8080 REQUEST_METHOD=GET SCRIPT_NAME=-e

    Now run this again:

    plackup -e'sub { [200,[],[q/ok/]] }'

    And you'll see that instead of spawning an HTTP server and printing out its address for you, plackup now behaves like a CGI script, printing out the response headers and body to STDOUT.

    plackup (or rather the guess sub defined in Plack::Loader) detects that it's running in a CGI environment and behaves appropriately.