PSGI25 Apr 2011
Dancer is the hottest new Perl PSGI-compatible web framework. You might be thinking, Perl web development? Doesn’t that use CGI? Isn’t CGI balls slow? Well…you’re wrong. Most things you think you know about Perl are probably wrong.
Let’s start out with debunking the myth of CGI. CGI is slow, but there are a number of Perl-compatible alternatives.
The most well-know, and best supported alternative is FastCGI. Don’t hate on FastCGI because it has CGI in the name, it avoids the big pitfall of CGI (loading a new interpreter every time a new page is called) by having a FastCGI server load your interpreter, and then responding to requests sent to the FastCGI server from the web server.
This is gives us a clear performance improvement (not having to load a new interpreter every time you load up a web page), as well as a security benefit (total isolation from the web server user/process).
The other well-known alternative to CGI is mod_perl. mod_perl is Apache, specific, and it’s also pretty old and janky. I’m not going to talk about it, mainly because I have never used it. It’s poorly documented, and there exist better alternatives anyway. Let’s move on.
Ahh, PSGI. We love PSGI. PSGI is fast, and is under ridiculously rigorous development. PSGI applications get a hashref that provides all of the necessary information about the environment: request method, uri, variables, protocol info, etc. It then returns an arrayref of a status code, HTTP headers and the document body.
Most of the time, you’ll be using a framework, so you’ll never have to care about interacting with the internals of PSGI directly.
Astute readers are probably thinking, “Hey, if PSGI is perl, then what does that run on, and how does it interact with my web server?” Well, good reader, it runs on any of the above-mentioned frameworks and more! There’s an Apache mod for it, it works with CGI, FastCGI, and mod_perl, and you have have to deal with any of that shit!
Plack is the reference implementation of the PSGI middleware. It sits between your webserver and Perl application serving up the PSGI environment for you, while translating the arrayref you return into meaningful data for the web server.