[ Perl tips index ]
[ Subscribe to Perl tips ]
A few months ago, we described how to write a twitter clone using
the Dancer framework.
Today, we're going to learn how we can use Plack to enhance
that application, without needing to change the application at all.
Plack is a set of utilities that work with the Perl Server Gateway Interface (PSGI) specification, which describes how web servers and applications communicate. Any application that adheres to the PSGI specification can be used with Plack.
In this tip, we're going to use our existing trillr application; a basic twitter clone that we wrote in a previous Perl tip. You can grab a copy of it with git (no authentication necessary):
git://github.com/pjf/trillr.git
Because our application is written with Dancer, we don't need
to make any changes at all to use the Plack framework. Provided
we have Plack installed, we can start it with:
$ plackup trill.pl
By default, we'll get a message like:
HTTP::Server::PSGI: Accepting connections at http://0:5000/
The HTTP::Server::PSGI is a reference implementation webserver
that's perfect for development. However you can also run your
Plack-wrapped applications in Apache and other servers, too.
One of the biggest reasons for using Plack is the large amount of Plack middleware that can be used with any PSGI application.
These middlewares can do anything from authentication, caching, stream compression, logging, or anything else that you can think of between content passing from an application to the client (or vice-versa).
One of the most useful layers is the Plack::Middleware::Debug
layer, which dynamically adds extra debugging to any PSGI application.
Since Dancer is already Plack-aware, we can load middleware layers just by modifying our config.yml file:
plack_middlewares:
-
- Debug
Now, if we run our application with plackup, each page has a
debug pane added to the right. By default, it allows us to
examine the environment, response, time spent, memory usage, and
a full database trace:

That's right, a full database trace, right out of the box:

In fact, we can customise the panels that appear in the debugging pane,
and even write our own! For example, if we had the Dancer::Debug
distribution installed, we could use:
plack_middlewares:
-
- Debug
- panels
-
- Environment
- Response
- Timer
- Memory
- DBITrace
- Dancer::Version
- Dancer::Settings
- Dancer::Logger
Of course, we can insert multiple middleware layers if we wish. The following configuration will also compress output before serving it, saving on bandwidth:
plack_middlewares:
-
- Deflater
-
- Debug
- panels
-
- Environment
- Response
- Timer
- Memory
- DBITrace
- Dancer::Version
- Dancer::Settings
- Dancer::Logger
Note that order that middleware is loaded is important. Modules lower on the list are run "closer" to the application. In our case, the Debug layer can only wrap uncompressed content, so we run it first before passing its output "upwards" to the Deflate layer and webserver.
We've just added some very nifty layers to our existing Dancer
application, but the whole point of Plack is that it's framework
agnostic. Provided your application can be made to talk the PSGI
specification, it can interface with Plack and Plack middlewares.
At the time of writing, all major Perl web frameworks (Dancer, Catalyst,
Mojolicious, CGI::Application, Jifty, etc) support Plack. If you're
working with legacy CGI code, then the CGI::PSGI and
CGI::Emulate::PSGI modules will allow you to convert to using
Plack/PSGI in just a few lines of code.
The Plack webpage, which also links to the resources below, should be your first port of call.
Tatsuhiko Miyagawa, the author of Plack, has released a Plack handbook. The source code is available for free, and the compiled ebook for a very modest fee.
The Plack Advent Calendar has 24 days of tricks and tips for using Plack
[ Perl tips index ]
[ Subscribe to Perl tips ]
This Perl tip and associated text is copyright Perl Training Australia. You may freely distribute this text so long as it is distributed in full with this Copyright noticed attached.
If you have any questions please don't hesitate to contact us:
| Email: | contact@perltraining.com.au |
| Phone: | 03 9354 6001 (Australia) |
| International: | +61 3 9354 6001 |
Copyright Perl Training Australia. Contact us at contact@perltraining.com.au