http://qs321.pair.com?node_id=703576

Squatting::On::Catalyst

(This was originally posted to the Catalyst Mailing List. It's being reposted on PerlMonks for the benefit of the broader Perl community.)

Squatting is a web microframework for Perl that's based on Camping from the Ruby world. During the transition from Ruby to Perl, Squatting gained some mutant powers which is why I'm writing to you today.

One of Squatting's mutant powers is its ability to embed itself into other frameworks (like Catalyst). You can take whole Squatting apps, and embed them into your existing web application with just a few lines of glue code. Allow me to demonstrate.

STEP 1: Install Pod::Server from CPAN.

$ sudo cpan Pod::Server

STEP 2: Run Pod::Server so you can see what it looks like.

$ squatting Pod::Server -p 8088
or
$ pod_server

Then, visit http://localhost:8088/ and pay attention to the URLs as you click around. (This will be interesting when you see how the URLs of the whole site will be "relocated" later on in the demonstration.)

STEP 3: Create a Catalyst application.

$ catalyst.pl Propaganda

STEP 4: Embed Pod::Server into the Catalyst application.

$ cd Propaganda $ $EDITOR lib/Propaganda/Controller/Root.pm

Then, add the following lines of code to an appropriate place in the controller.

use Pod::Server ’On::Catalyst’; Pod::Server->init; Pod::Server->relocate(’/pod’); $Pod::Simple::HTML::Perldoc_URL_Prefix = ’/pod/’; sub pod : Local { Pod::Server->catalyze($_[1]) }

STEP 5: Start up the Catalyst application.

$ script/propaganda_server.pl

Finally, visit http://localhost:3000/pod/. If everything worked, you should see that Pod::Server has just been embedded into a Catalyst application.

What Are The Implications of This?

Right now, anyone who is developing a web app with Catalyst can install Pod::Server from CPAN and embed it directly into their web app. However, this is just the beginning.

Imagine if you could install a blog, a wiki, a forum, or a store just as easily.

Imagine adding major functionality to any Perl-based site with ~5 lines of code.

Squatting makes modular web applications both possible and probable.

http://search.cpan.org/dist/Squatting/

http://github.com/beppu/squatting/tree/master

Replies are listed 'Best First'.
Re: Squatting::On::Catalyst and Truly Modular Web Apps
by MidLifeXis (Monsignor) on Aug 11, 2008 at 17:10 UTC

    I agree with the marketing / branding side presented by the AM above, although I probably would not use the given example :)

    Impression, correct or not, can taint many decisions. "Squatting" has the impression of illegitimacy, lack of blessing, or possibly even stealing. "Camping" implies at least permission, if not promotion. In today's BSA skittish world, staying away from the potential (actual or not) of illegitimacy can't hurt.

    The merits of the module appear to be very good, but I would hate to see it dismissed before looking at the merits because of the name.

    --MidLifeXis

Re: Squatting::On::Catalyst and Truly Modular Web Apps
by beppu (Hermit) on Aug 13, 2008 at 20:44 UTC

    I hope...

    I hope someone out there thought that Squatting was a funny name for a port of the Camping web microframework. Keep in mind that I am aware of the various (vulgar) connotations that a word like "squatting" has -- that's why I picked it! Maybe my sense of humor is nerdy beyond repair, but I thought it was funny.

    (What have our corporate overlords done to us?)

      I hope someone out there thought that Squatting was a funny name

      I thought it was both a hilarious name and a badass piece of software :)

      -stvn
Re: Squatting::On::Catalyst and Truly Modular Web Apps
by metaperl (Curate) on Aug 14, 2008 at 15:29 UTC
    I like Squatting quite a bit John. It seems about as nice as my favorite CGI::Prototype. I have some comments to make about the docs here.

    controller did model things

    In the SYNOPSIS where you show a basic app, we see the following in the controller:
    # STEP 2 => Create a Controllers package { package App::Controllers; use Squatting ':controllers'; # Setup a list of controller objects in @C using the C() function. our @C = ( C( Home => [ '/' ], get => sub { my ($self) = @_; my $v = $self->v; $v->{title} = 'A Simple Squatting Application'; $v->{message} = 'Hello, World!'; $self->render('home'); }, post => sub { } ), ); }
    But I dont think $v->{title} or $v->{message} are view. They should have been pulled from the model by the view. And they certainly dont belong in the controller, whose only job is to pick a particular model and view and let them do the work.

      Hey, man. It's been a while. Hope you're well. :)

      Anyway...

      You bring up a good point. $self->v is the equivalent of Catalyst's stash. However, now that you bring this up, I think "v" was not the best name for this hashref. The mnemonic I had in mind was "v" for "variable", but I can see how it can be ambiguous with "V" for "View". Ah fuck. ;-)

        I think "v" was not the best name for this hashref. The mnemonic I had in mind was "v" for "variable", but I can see how it can be ambiguous with "V" for "View". Ah fuck. ;-)
        heh. maybe there could be an alias like $self->stash

        But I think Brock or you would be in a better position to actually decide on the verbose descriptive non-deceptive alias than me.

Re: Squatting::On::Catalyst and Truly Modular Web Apps
by kingkongrevenge (Scribe) on Aug 13, 2008 at 23:09 UTC
    What's it for beyond Pod::Server? What other possible application does it have?

    Why would I want to embed Pod::Server?

    I think this is going over my head.

      Pod::Server is just being used as an example. There's no practical purpose to embedding Pod::Server into anything. I'm just trying to show you guys that the embedding functionality works. The next step if for you guys to use your imagination and try to see a future where you can build significant portions of a site by combining multiple applications together into one cohesive whole.
Re: Squatting::On::Catalyst and Truly Modular Web Apps
by metaperl (Curate) on Aug 14, 2008 at 15:32 UTC
    Of course, the real question is: can it do a wiki in 20 lines or less :)

    But seriously, is there some sort of shootout where we see how a web framework handles certain application tasks? Implementing petshop was done by Bivio to compare with the Java petshop, but that is a rather large task.

      A 17-line Wiki

      Prerequisites

      • Squatting
      • Text::Textile
      • IO::All

      Code

      package MicroWiki; use base 'Squatting'; package MicroWiki::Controllers; use Squatting ':controllers'; use IO::All; @C = C( Page => ['/', '/(\w+)', '/(\w+).(edit)' ], get => sub { $_[1] ||= 'Home'; -f $_[1] || 'Edit' > io($_[1]); $x < io +($_[1]); $_[0]->v->{page} = $_[1]; $_[0]->v->{text} = $x; $_[0]->render($_[2] && 'edit' || 'page') }, post => sub { $_[0]->input->{text} > io($_[1]); $_[0]->redirect(R('Page', $_[1])) }) +; package MicroWiki::Views; use Squatting ':views'; use Text::Textile qw(textile); our @V = (V(html, page => sub { '<a href="'.R('Page',$_[1]->{page},'ed +it'). '">edit</a>'.textile($_[1]->{text}) }, edit => sub{sprintf( ' <form method="post" action="%s"><textarea name="text" rows="20">%s</te +xtarea>' .'<input type="submit"/></form>', R('Page', $_[1]->{page}), $_[1]->{text}) })); 1;

      And no line exceeds 78 characters in length. ;-) I can actually shrink this by a few more lines if I tried harder, but I'm satisfied w/ what I have.

      To run this Wiki, put the code in a file called "MicroWiki.pm" and type:

      squatting MicroWiki

      To embed this into a Catalyst app, add the following code to your Catalyst app's Root controller.

      use MicroWiki 'On::Catalyst'; MicroWiki->init; MicroWiki->relocate('/wiki'); sub wiki : Local { MicroWiki->catalyze($_[1]) }

      If you want to replace the (non-existent) layout:

      my $view = $MicroWiki::Views::V[0]; $view->{layout} = sub { my ($self, $v, $content) = @_; # return a string that wraps around $content };
        Runs great right out of the box. I have it up at http://ns4.epfarms.org:4234/ if anyone wants to give it a go. I still vote for optional alternate longer names for C(...) and V(...)... yeah yeah patches welcome :)
        A 17-line Wiki
        sa-weet.
Re: Squatting::On::Catalyst and Truly Modular Web Apps
by metaperl (Curate) on Aug 15, 2008 at 14:48 UTC
    Ok, so can we have a google group to discuss Squatting on?
A reply falls below the community's threshold of quality. You may see it by logging in.