Explaining Web Programming via Plack

I'm currently writing Chapter 15 of my Beginning Perl book and it's about Web programming. The first part is about server-side software and the second part is about clients.

When I finally sat down to write about Web applications, I thought of what I should do. Some of you may remember my old Web programming course, but that was written around CGI and just wouldn't do. So I need to use something modern, but since I have a deadline, that means writing about something I know fairly well. That seemed to leave me with two primary options: Catalyst or Dancer. The latter is easier to use, but still "magical" enough to hide things I wanted to explain. Exploring other options would mean learning to use them and possibly missing a deadline.

That's when inspiration struck.

My reviewers had initially pushed hard for me to include Plack in the book, but I wasn't sure. After all, this is an introductory book and trying to explain how Plack sits between Web servers and Web applications seemed, well, a bit more than I was trying to do.

However, I know Plack fairly well and decided that it would be easy enough to use. Take a look at its Hello World:

my $app = sub {
    return [
        200, 
        [ 'Content-Type' => 'text/plain' ],
        [ 'Hello World' ]
    ];
};

Save that as app.psgi and run plackup and you have a Hello World app in your browser.

Dead simple, eh? On top of that, while it's easy to use, it's also low-level enough that you can clearly see the HTTP response code, HTTP headers and the HTTP body.

I make it repeatedly clear that my examples are toy examples. Plack is intended to be a platform one builds on, not uses directly, but it's a perfect blend between showing the process of building a Web application and the underlying HTTP that binds the client and server. Since this chapter is an introduction to the concepts in Web programming and not an actual tutorial (though all of the examples run), I think Plack hits the sweet spot here.

Color me pleased.

9 Comments

"Plack is intended to be a platform one builds on, not uses directly" : exactly.

Plack is probably an excellent choice to introduce the Web mechanism with a good balance between very low level and too high level.

That said, I don't think people develop solely in Plack. And I would be sad to see only Plack taught in your book. Web Programming is (in my understanding) a layer higher. So Dancer or Catalyst, for instance.

I'd say an introduction to the concepts with Plack, then an introduction to Dancer / Catalyst, while explaining how they can rely on (and provide access to) Plack, would be the best. Personally I'd use Dancer to demonstrate, and point to Catalyst to go further. But that's just me :)

Hi Ovid,

Probably too much but you might want to have a look at the plack bit of:

http://www.ibm.com/developerworks/aix/library/au-perlweb_revision/index.html

Which you may or may not find useful - enjoy :)

Leo

In the Perl class I taught last year, newbies really liked straightup Plack, and I found it let me concentrate on teaching Perl, instead of teaching a web framework.

When I did reach for a framework I used Web::Simple, which is even more micro than Dancer. That also went pretty well, and again it let me concentrate on teaching Perl and programming concepts instead of teaching a web framework. I'd be happy to share code and slides if you'd like, just give me a shout!

Johnn

Hi Ovid

On this page Perl articles I've written a couple of articles as intros to Plack.

Feel free to copy any material you see there.

"Plack is intended to be a platform one builds on, not uses directly": Perhaps not, or perhaps it is easy and effective to use directly despite the original intent.

Direct use of Plack might be thought of opting for a "library" as opposed to a "framework." While it may be said that Dancer is more appropriate for Ovid's target audience of new learners, since one can install the Dancer distribution and immediately have everything needed to build a dynamic web application, Plack provides similar, "batteries-included" amenities: middleware for logging and authentication, server shims, standalone servers.

The main thing that plain old plack won't have out of the box is some sort of built in templating. This is easy to graft in, but is not really a documented part of the ecosystem, since typically we think of plack as living at the base of the stack.

When I used Plack in my class I did simple templates using HTML::Tiny, which again being just Perl that meant I didn't need to teach a templating system. For forms I used HTML::FormHandler which has reasonable sane built in html rendering. That was nice and made it so I could avoid having to teach much html at the same time.

About Ovid

user-pic Freelance Perl/Testing/Agile consultant and trainer. See http://www.allaroundtheworld.fr/ for our services. If you have a problem with Perl, we will solve it for you. And don't forget to buy my book! http://www.amazon.com/Beginning-Perl-Curtis-Poe/dp/1118013840/