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

"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.