
Squatting::Cookbook - Web Development Techniques for Squatting

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 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.
package App; use base 'Squatting'; our %CONFIG = ();
If your app is called App, then:
App::Controllers.
use Squatting ':controllers'.
@C. package App::Controllers;
use Squatting ':controllers';
our @C = (
C(
Home => [ '/' ],
get => sub {
my ($self) = @_;
$self->render('home');
}
),
C(
Profile => [ '/~(\w+)' ],
get => sub {
my ($self, $name) = @_;
my $v = $self->v;
$v->{name} = $name;
$self->render('profile');
}
)
);
If your app is called App, then:
App::Views.
use Squatting ':views'.
@V. package App::Views;
use Squatting ':views';
our @V = (
V(
'html',
layout => sub {
},
home => sub {
},
profile => sub {
},
)
);

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.
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 $app->SUPER::service, we have to write $app->next::method, because Squatting is a sublcass of Class::C3::Componentised.
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.
App->mount('AnotherApp', '/prefix');
In order to embed a Squatting app into an app written in another framework, we need to be able to do the following things.
If we can do all these things, Squatting can make itself at home. Here are some concrete examples to get you started.
To embed a Squatting app into a Catalyst app, you can add code like this to your Root controller.
use App 'On::Catalyst';
App->init;
App->relocate('/somewhere');
sub somewhere : Local { App->catalyze($_[1]) }
If you want the Squatting app to be completely in charge, you don't even have to relocate() -- just redefine the default() method like this:
use App 'On::Catalyst';
App->init;
sub default : Private { App->catalyze($_[1]) }

This is the simplest thing you could possibly do, but it's also somewhat limiting.
If you've embedded a Squatting app into another application, the rules and conventions governing the other application's framework take precedence. Follow their deployment guidelines, and you should be fine.

This section is for those who wish to scale Squatting apps that are using a Continuity foundation. If any part of your site is RESTless and stateful, and you've suddenly got a lot of traffic to your site, this section is for you.
TODO
TODO
This is currently science fiction.