The 140 character webapp
Anyone catch this The 140 Character WebApp Challenge” on Twitter recently?
f055 as been able to create a webapp in Perl that can be posted in a Tweet! Pretty impressive.
What also caught my eye was that some of the responses were using micro webframeworks like Camping & Sinatra (despite original challenge saying only using standard libraries!).
Still fair game… so I’d thought I’d give it a shot with Squatting. Here’s a Squatting version of the f055 micro-twitter webapp…..
package W;use base 'Squatting';package W::Controllers;use Squatting':controllers';@C=C(w=>['/'],get=>sub{($s)=@_;unshift@w,$s->input->{m};$s->v->{m}=\@w;$s->render('w')});package W::Views;use Squatting':views';@V=V('',w=>sub{my $r;$r.="<hr>$_"for@{$_[1]->{m}};return'<form><input name=m>'.$r});1;
Nice eh 😉 Just type “squatting W.pm” and away u go. And like Sinatra it has its own webserver! (Squatting runs by default on Continuity).
So weighs in at 297 chars so too big for Twitter 😦 Still it does well against the already published 308 chars Camping version but still a way to go against the two published Sinatra versions with 140 & 136 chars.
So can it be trimmed? Not easily with those package declarations. But how about a Continuity version!?
use Continuity;Continuity->new->loop;sub main{($r)=@_;while(){$r->print('<form><input name=m>');$r->print('<hr>',$_)for @b;$r->next;unshift@b,$r->param('m')}}
This comes in at a very respectable 159 chars and with its own webserver (and just by doing perl program_name.pl).
However the keen eyed of you may have noticed something different about both Squatting & Continuity scripts compared to what has already been so far posted in the challenge? Mine are not saving state anywhere (ie. to a file). And even more remarkable fire up Squatting app in another web browser and you see the state appear in both browsers.
Unfortunately though all this state is maintained in memory (in package variables in Squatting’s case). So once these webapps are stopped then all is lost 😦 So to retain state after a restart you would have to revert to saving data to file. Here’s an amended Continuity version….
use Continuity;Continuity->new->loop;sub main{($r)=@_;while(){$r->print('<form><input name=m>');open F,'+<0';$r->print('<hr>',$_)for reverse<F>;$r->next;print F$r->param('m'),"\n"}}
BTW. Writing to file in a webapp isn’t a good idea. You have to be careful of race conditions and locks. Continuity uses co-routines so it should be OK compared to normal CGI stuff (I think?).
So now this Continuity micro-twitter comes in at 183 chars. Not bad… but still not Twitter compliant 😦
Still challenge was a Twitter sized “webapp” and so here is a simple number accumulator written in Continuity….
use Continuity;Continuity->new->loop;sub main{($r)=@_;while(){$r->print('<form><input name=m>');$r->next;$x+=$r->param('m');$r->print($x)}}
And it comes it right on the mark at 140 chars 🙂
/I3az/
Nice post Barry! I like how you detailed the road to 140 chars. Thanks for taking the challenge! It’s just a couple days left and I’ll be doing the final wrap up 🙂
Thanks Marek.
And well done to yourself for an inspired challenge & blog post.
I’ve tweeted the Continuity web app…..
… it looks a bit like a work of art now 🙂
chars Barry
Very impressive.
I think you could shave 3 more characters by rewriting the while loop (this is untested):
use Continuity;Continuity->new->loop;sub main{($r)=@_;$r->print(”),$r->next,$x+=$r->param(‘m’),$r->print($x)while1}
Also, thanks for the intro to Continuity and Squatting. They both look very interesting.
@prakash – Thanks very much.
I did also post a an improved version on f055.net comments weighing in at 117 chars…. http://f055.net/article/the-140-character-webapp-challenge/P50/
Here it is to save wading thru comments….
BTW… I assume your code got mangled a bit when posted because its missing “form input” part and the while loop doesn’t quite work. But yes it does show yet another way to write this number counter webapp in under 140 chars using Perl/Continuity. Thats three at last count 🙂
/I3az/