=head1 NAME Squatting::Cookbook - Web Development Techniques for Squatting =head1 INTRODUCTION Squatting exists because I fell in love with Camping's API, and I couldn't bear the thought of building another site using some other API. When I decided that the next site I wanted to build would be implemented in Perl, I had no choice but to port Camping from Ruby to Perl, and that's how Squatting was born. My hope is that other Perl programmers will be able to appreciate how concise this API is, and I hope they'll see just how far a little bit of code can go. =head2 The Anatomy of a Squatting Application For many of the examples to follow, a Squatting app called "App" will be used. First, let's take a look at how the packages are laid out. +-----------------------------------------------------------------+ | App | | init() one-time initialization | | service() run on every HTTP request | | | | %CONFIG app configuration goes here | | | | +-------------------------------------------------------------+ | | | App::Controllers | | | | @C list of controller objects goes here | | | | | | | | C() utility function for making controllers | | | | R() a URL generation function | | | | | | | +-------------------------------------------------------------+ | | +-------------------------------------------------------------+ | | | App::Views | | | | @V list of view objects goes here | | | | | | | | V() utility function for making views | | | | R() a URL generation function | | | | | | | +-------------------------------------------------------------+ | +-----------------------------------------------------------------+ Most Squatting apps will be composed of at least 3 packages. =over 2 =item First you need a package for the App itself. B will inherit from L. Override the C method if you want to perform some initialization upon start up. Override the C method if you want to do something before or after an HTTP request. Put your app config in %CONFIG. =item Then you need a package to hold the controller objects. B should C and populate @C with controller objects. package App::Controllers; use strict; use warnings; use Squatting ':controllers'; our @C = ( C( Home => [ '/' ], get => sub { } ), C( Contact => [ '/contact' ], get => sub { }, post => sub { }, ), ); =item Finally you should have a package for holding the view objects. B should C and populate @V with view objects. package App::Views; use strict; use warnings; use Squatting ':views'; our @V = ( V( "default", layout => sub { }, home => sub { }, contact => sub { }, ), ); =back Keep in mind that the packages for controllers and views are not classes. They are never instantiated. Think of them more as namespaces, because that's the way Squatting interprets them. Their main purpose is to be containers for objects. B A reductionist might say that an app is just an object that contains a list of controller and view objects, and he wouldn't be that far off from the truth. This design decision is what allowed Squatting to embed itself just about anywhere. Being a piece of data means that you're free to move around, and this is what gave this framework a lot of unintended power. =head1 PROGRAMMING TECHNIQUES =head2 COMET The easiest way to add COMET support to a Squatting app is to install L via CPAN and mount it. Stardust is a Squatting app that implements a simple COMET server that's intended to run alongside any old regular web app. It provides a RESTful API so that even people who don't use Perl can use it as their COMET server and pass realtime messages around. However, if you're using Stardust with a Squatting app, you get to bypass the RESTful API and send messages into the system directly. B: sudo cpan Stardust B: perldoc Stardust stardust.pl --help stardust.pl --demo If you want to understand how it works, take a look at the code. It's a very small Squatting app that only has a few controllers, and it'll acquaint you with the wonders of L and L. B: L =head2 How to Set Up Sessions (I could actually use a little coding help here. If someone could take the time to write a plugin called L that would be great.) =head3 Continuity and Process Memory Pure Continuity apps typically don't use persistent session storage, because they can use lexically scoped variables instead. However, Squatting apps are RESTful and stateless by default, so you can't count on the lexical scope of a controller to stick around between requests. Luckily, package variables *will* stick around, so that's what we'll use to implement persistent sessions. package App; our %state; sub service { my ($app, $c, @args) = @_; my $cr = $c->cr; my $sid = $cr->{session_id}; if (defined $sid) { $c->state = $state{$sid} ||= {}; } $app->next::method($c, @args); } Here, we override service() in the main module of our app so that $c->state will provide a hashref whose values will persist between requests. Note that instead of writing C<$app-ESUPER::service>, we have to write C<$app-Enext::method>, because Squatting is a sublcass of L. =head3 When Squatting::On::Catalyst When squatting on top of Catalyst, the Catalyst session becomes C<$self-Estate> in Squatting. The session storage code in Catalyst is very mature, so it is highly recommended that all the session setup be done on the Catalyst side. =head3 Sessions From Scratch The challenge is to find a way to assign unique session ids to each visitor and use that session id as a key into a persistent store. TMTOWTDI =head2 How to Use Various Templating Systems With Squatting =head3 HTML::AsSubs I like L for the following reasons: =over 4 =item * It works as advertised. =item * The implementation is really small. =item * It seems to be widely deployed (even though no one uses it). =item * And generating HTML with code eliminates the need to install template files. =back The documentation is up-front about some of the module's shortcomings which I appreciate. However, the docs go a bit too far and recommend that this module not even be used! It says that there are "cleaner" alternatives, but when I looked at them, I came straight back to HTML::AsSubs. I think the module works just fine, and I'd like to show you how I use it. =head4 Addressing HTML::AsSubs Shortcomings (Alleged and Otherwise) =over 4 =item The exported link() function overrides the builtin link() function. Noted. You shouldn't be calling the builtin C in view code anyway, so it's not a big deal. =item The exported tr() function must be called using &tr(...) syntax. This is because it clashes with the builtin tr/../../ operator. I can live with this. =item Problem: exports so damned much. (from the code comments) The funny thing is, it's actually not exporting enough. It's missing subs for the C, C, and C tags. sub span { HTML::AsSubs::_elem('span', @_) } sub thead { HTML::AsSubs::_elem('thead', @_) } sub tbody { HTML::AsSubs::_elem('tbody', @_) } If there are any other missing tags, you know what to do. =back There's one more pseudo-tag that I like to add for practical reasons. sub x { map { HTML::Element->new('~literal', text => $_) } @_ } Normally, HTML::AsSubs will entity escape all the text that you give it. However, there are many times when you legitimately don't want text to be entity escaped, so that's what C is for. =head4 An Example View That Uses HTML::AsSubs package App::Views; use strict; use warnings; use Squatting ':views'; use HTML::AsSubs; sub span { HTML::AsSubs::_elem('span', @_) } sub thead { HTML::AsSubs::_elem('thead', @_) } sub tbody { HTML::AsSubs::_elem('tbody', @_) } sub x { map { HTML::Element->new('~literal', text => $_) } @_ } our @V = ( V( 'html', layout => sub { my ($self, $v, $content) = @_; html( head( title( $v->{title} ), style(x( $self->_css )), ), body( x( $content ) ) )->as_HTML; }, _css => sub {qq| body { background : #000; color : #f5deb3; } |}, home => sub { my ($self, $v) = @_; h1( $v->{message} )->as_HTML; }, ), ); 1; Again, the nicest part about generating HTML from code is that you don't have to worry about installing template files. The templates are in memory as perl expressions. When building web apps that are designed to be embedded, this is a really nice feature to have as it makes deployment that much easier. If HTML::AsSubs is a bit too low tech for you, there are more modern expressions of the code-to-html idea on CPAN. For example, L and L may be worth looking into. I'm happy with L, though. =head3 Tenjin Tenjin is the fastest templating system that no one outside of Japan seems to know about. It's really unfortunate that this module isn't on CPAN, but hopefully this will be rectified in the near future. Until then, you can download it from L. =head4 An Example View That Uses Tenjin First, make sure your template_path is configurable for deployment purposes. package App; our %CONFIG = ( template_path => './www' ); And here is the actual view: package App::Views; use strict; use warnings; no warnings 'once'; use Squatting ':views'; use Tenjin; # make functions defined in this package available to templates use base 'Tenjin::Context'; eval $Tenjin::Context::defun; $Tenjin::CONTEXT_CLASS = 'App::Views'; our @V = ( V( 'tenjin', tenjin => Tenjin::Engine->new({ path => [ $App::CONFIG{template_path} ], postfix => '.html' }), layout => sub { my ($self, $v, $content) = @_; my $tenjin = $self->{tenjin}; $v->{content} = $content; $tenjin->render(":layout", $v); }, _ => sub { my ($self, $v) = @_; my $tenjin = $self->{tenjin}; $v->{self} = $self; $tenjin->render(":$self->{template}", $v); } ), ); 1; That's all there is too it. Views for other file-based templating systems will follow a similar pattern where the special C<_> template is used to map method names to filenames. =head3 Template Toolkit L