|There's more than one way to do things|
Meet Joe Plackby Logicus
|on Oct 20, 2011 at 05:19 UTC||Need Help??|
Of all the web development frameworks and systems I've played with over the last few years, Miyagawa's PSGI/Plack system is by far the most intuitive, powerful and high performing by an embarrassingly wide margin.
It's relatively new, and already it's turning heads all over the the perl world, simply because it is possibly the best thing that has ever happened to Perl, and you will be doing yourself a huge favour by getting to know and love this little gem of a system.Installation
Plack is available on CPAN, simply install it by typing
Plack has many many dependencies, and you will be sat there pressing 'y' for quite some time so be prepared for that. It is well worth the wait and so far it has installed and compiled correctly for me using Ubuntu 10.10 (Maverick) every time so there isn't likely to be any problems for you either unless you have some really bizarre setup.Getting Started
The first thing to understand about Plack is that it is a pure Perl HTTP server which completely replaces your existing server such as Apache.
Task::Plack comes with another module which to give it it it's full name is called HTTP::Server::PSGI::Net::Server::PreFork, which is rather long and so to save you from getting RSI has been renamed simply to 'Starman'.
This thing is a pre-forking high performance HTTP server which will give you the ability to send thousands of requests per second with a decent processor such as a Quad Core AMD Phenom II. Also the memory usage is extremely low meaning the server can serve more simultaneous users without grinding around in virtual memory.Staring to code for Plack/PSGI
In the PSGI way of doing things, responses always take the form of a 3 element array :
Taking these elements in order, we have; first the HTTP status code such as 200 ('ok') or 404 ('not found'). Second comes the content type declaration, such as text/html or text/plain. This is equivalent to the old way of doing it such as :
Then we have the 3rd and final element of the array which is the body of the response and may either be a string or an array of strings.Your first app
Ok, so now we want to actually get a working app going so we can see that everything is ok and has installed correctly etc. Plack takes a coderef for input and compiles it into memory, hence the blazing performance. So open your favourite editor and create a new file, for instance 'action.psgi'
Save that file and then from the command line we invoke the server and load the app by running the command 'plackup'
The server should tell you it is now accepting requests at localhost:5000 . Direct your browser there and if all has gone according to plan you should get the result displayed in your browser :
For more information, I suggest doing a google search for "plack advent calendar" and I will be back with more examples and patterns when I have some more spare time to rant on about this absolutely awesome piece of kit!