Preamble

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

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 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 :

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 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'

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!


In reply to Meet Joe Plack by Logicus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.