Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Meet Joe Plack

by Logicus
on Oct 20, 2011 at 05:19 UTC ( #932566=perlmeditation: print w/ replies, xml ) Need Help??

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!

Comment on Meet Joe Plack
Select or Download Code
Re: Meet Joe Plack
by anneli (Pilgrim) on Oct 20, 2011 at 05:30 UTC

    Thanks for the tutorial! I should probably take a look at Plack soon, looks like some fun.

    Miagawa's

    By the way, his name is spelled Miyagawa.

      Thanks, edited!
Re: Meet Joe Plack
by nando (Acolyte) on Oct 20, 2011 at 06:30 UTC

    Years ago I use Perl to solve small problems at work, topography, from data formatting of "cross sections" from a spreadsheet to AutoCAD to the implementation of simple algorithms "numerical methods".

    The result is that I have very limited use of the power of Perl. I lack a deep understanding of different programming paradigms that supports Perl to further leverage the official Perl documentation.

    That means there are two ways to penetrate the capabilities of Perl: the documentation, they need those who move easily in different environments of"Perl world" and only need to know how the new module is integrated into the general abstract model, and tutorials, essential for those who try to penetrate the mysteries of the abstract model.

    In my situation, what I try, precisely, is to learn Perl using Perl. Use it to replace my old bash scripts to manage my system. Use it to make those little math now entrust to Octave, or to manage this small database that need my brother ... Or ...

    Acting thus gain the freedom I need to do so. A loopback

    Right now I'm reviewing some of the basics of Perl necessary to approach Mojolicious. Why Mojolicious? ... For documentation. For his tutorials. By their casts. Because thanks to them I could understand the MVC model as well as Mojolicious. And because the simple CGI I've done to that database from my brother was talking about earlier, has left me with the desire to understand how it's done, really, this kind of work. ^_^

    Acting thus gain the freedom I need to do so.

    For me, "Intermediate Perl" is the paradigm to follow. Learning means finding a problem, understand the problem, evaluate possible solutions and implement the best of them. And the context, which is taken for granted so often in the official documentation on many modules, it is everything.

    I would like to encourage you to develop a tutorial on the terms described. Many thanks you ;).

    a greeting

Re: Meet Joe Plack
by Limbic~Region (Chancellor) on Oct 20, 2011 at 13:05 UTC
    Logicus,
    Plack has many many dependencies, and you will be sat there pressing 'y' for quite some time so be prepared for that.

    Unless you have a reason to monitor every single module that is installed, why not just:

    o conf prerequisites_policy follow o conf commit

    Cheers - L~R

      where would you type the above, at the CPAN prompt?
Re: Meet Joe Plack
by sundialsvc4 (Monsignor) on Oct 21, 2011 at 13:49 UTC
    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.

    Strictly speaking, that statement is not quite true ... that is to say, there might be a better way to say it.   If I may now brashly presume to try.

    Plack is a sort of “software ball-and-socket joint” that allows your web application to run in a variety of possible environments, under many different web-servers including the plackup command-line “server,” without being dependent on any one of them.   It allows you to “un-wedge” your application from any server-specific dependencies that it might now have.   To completely butcher my analogy, “if you will make your app compatible with the Plack ‘ball,’ then you become free to choose the pre-existing ‘socket(s)’ of your choice.”   (I am making no reference to TCP/IP “sockets” here ...)

    I recently used Plack to redeploy a crufty old mod_perl application into FastCGI.   (You can read all about my troubles and whining and inevitable stumbling-around here on Perlmonks.)   To accomplish this, I built a new Plack-compatible entry point into the application which in turn called the legacy handler() routine, giving that handler a new parameter-object which was sufficiently compatible with what the legacy application expected. I also had to wedge-out and replace a few calls to Apache-specific API routines.   Direct calls to handler() no longer take place.   I built an app.psgi file that would invoke that routine (because that is what plackup looks for).

    “Okay, that’s the ball ... now, let’s build the socket.”   I prepared an off-the-shelf package that could be called by FastCGI, and I chose to arrange for it to look for app.psgi in the same way that plackup does.   Now, the application can easily be invoked either by FastCGI or by the plackup command, and/or by any other environment that we may one day select and prepare a “socket” for, and it never has to know the difference.   (Although it is capable of doing so, and for developer convenience it actually does.)

    Plans are now afoot to have portions of the app running under the nginix server “over here” while it is also running under Apache (through mod_fastcgi) “over there,” and no further application changes will be required to accomplish that.   We are also planning to use the Plack infrastructure to allow front-end application servers to converse separately with back-end servers ... exchanging information across CPU boundaries using convenient existing code that we know already works well.

    It is also worth noting that the concept of Plack isn’t new, nor is it unique to Perl.   Python actually did it first.   Ruby has “rack.”   It just happens to be a very good idea.   Plack is fast, efficient, well thought out, and well implemented.

      Nice, I'd give you the ++ if I wasn't currently locked in the monastery dungeon for my sins.
Re: Meet Joe Plack
by jdrago999 (Pilgrim) on Oct 21, 2011 at 17:57 UTC

    Plack rocks!!!!

      Your not wrong!
Reaped: Re: Meet Joe Plack
by NodeReaper (Curate) on Oct 22, 2011 at 12:34 UTC
Reaped: Re: Meet Joe Plack
by NodeReaper (Curate) on Oct 24, 2011 at 12:57 UTC
Reaped: Re: Meet Joe Plack
by NodeReaper (Curate) on Oct 26, 2011 at 12:53 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://932566]
Approved by moritz
Front-paged by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (16)
As of 2014-08-27 15:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (242 votes), past polls