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.
InstallationPlack is available on CPAN, simply install it by typing
sudo cpan install Task::Plack
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 StartedThe 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/PSGIIn the PSGI way of doing things, responses always take the form of a 3 element array :
return [ 200, [ 'Content-Type' => 'text/html'], [ 'hello PSGI world' ] ];
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 :
print "content-type: text/html\n\n";
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 appOk, 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'
listing of action.psgi ---------------------- my $app = sub { return [200, [ 'Content-Type' => 'text/plain'], [ 'Hello world' ] ]; };
Save that file and then from the command line we invoke the server and load the app by running the command 'plackup'
> plackup -s Starman -r action.psgi
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 :
Hello world
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Meet Joe Plack
by Limbic~Region (Chancellor) on Oct 20, 2011 at 13:05 UTC | |
by Logicus (Initiate) on Oct 20, 2011 at 19:53 UTC | |
by Limbic~Region (Chancellor) on Oct 20, 2011 at 20:09 UTC | |
by Logicus (Initiate) on Oct 20, 2011 at 20:45 UTC | |
by dwalin (Monk) on Oct 21, 2011 at 19:48 UTC | |
|
Re: Meet Joe Plack
by locked_user sundialsvc4 (Abbot) on Oct 21, 2011 at 13:49 UTC | |
by Logicus (Initiate) on Oct 21, 2011 at 18:35 UTC | |
|
Re: Meet Joe Plack
by nando (Acolyte) on Oct 20, 2011 at 06:30 UTC | |
|
Re: Meet Joe Plack
by anneli (Pilgrim) on Oct 20, 2011 at 05:30 UTC | |
by Logicus (Initiate) on Oct 20, 2011 at 05:58 UTC | |
|
Re: Meet Joe Plack
by jdrago999 (Pilgrim) on Oct 21, 2011 at 17:57 UTC | |
by Logicus (Initiate) on Oct 21, 2011 at 18:35 UTC |