Categories
Blather Comics Oddities Software and Programming

A new logo for Perl 6 (comic)

Not quite sure if this counts as a real comic or just a crappy collage (comics are hard), but inspired by recent efforts by more talented people, I came up with an idea for a new logo for the Perl6 project, which I think conveys of the spirit and vitality of the Perl community on the 10th anniversary of the Perl6 undertaking. Or not.
click to see the whole thing:
Click for full comic

Watchmen artwork and lettering is by the incomparable Dave Gibbons, Watchmen text is by the divine Alan Moore, Perl 6 Camelia logo copyright 2009 by Larry Wall, Sebastian Riedel‘s user icon and artwork are his. All images remixed without permission for obscure in-jokey satirical purposes.

I’ll just add that there are plenty of useful features introduced for Perl 6 and implemented in Perl 5; a decent logo/mascot/emblem would not be the least of them.

Categories
Blather Software and Programming

jQuery in Perl

A few months ago, I ran across a reference to a Perl module called pQuery, which is an attempt at porting jQuery to Perl. As modules go, it’s a derelict construction site. It tries to translate the jQuery Javascript library to Perl, replacing jQuery’s use of the browser DOM with HTML::Tree, which is not as easy as one could hope. It is also very out of date.

I had the bright idea of updating it, by translating the current version of jQuery to Perl. I decided to start with John Resig’s selector engine, Sizzle, which seemed like a nice stand-alone chunk with extensive tests.

I actually converted the whole file into something that could run in Perl, replacing the browser DOM with libXML this time, before realizing what a pointless exercise this was. First, because Sizzle tries to use higher-level browser APIs as much as possible, not low-level DOM calls (so, trying to figure out how it works by running it through firebug takes one down a different code path than the one used by my Perl code). Second, because at the high level, LibXML lets you use XPath expressions, which would be better than all this DOM grubbing.

John Resig had a blog post explaining why jQuery doesn’t convert its CSS selectors to XPath expressions to get maximum performance on browsers that support XPath, like other Javascript libraries do. Following his links, I found a nice-looking implementation in Prototype.js. I decided to drop my Perl version of Sizzle (pizzle?) and convert the Prototype.js stuff instead. When I got around to it.

Thinking about it again sometime later, I thought this code should live somewhere under HTML::Selectors. So I searched CPAN and found Miyagawa has got this covered (referencing this useful conversion table from Aristotle Pagaltzis. See also a trivial module that hooks up those selectors to an HTML parser, and an independant HTML::Tree-based version called HTML::Query from Andy Wardley (the Template Toolkit guy).

Oh, and there’s also Matt Trout’s HTML::Zoom, which is a templating engine using jQuery-like selectors to pick pieces of HTML to manipulate (and also has jQuery-like chaining).

Which means, someone has done the hard bit, now all that needs to be done is leveraging this into a jQuery-like tool for writing one-liners that spider and transform web pages like putty.

… and last week, Sebastian Riedel added support for DOM/CSS3 to Mojolicious and posted this one-liner on twitter:


"print Mojo::Client->new
  ->get('http://digg.com')
  ->success->dom->at('h3 a')->text;"

As sweet as that code is though, it’s a bit too fragile for the messy real-life web, because when I try to do the same trick on, say Hacker News, it fails:

print Mojo::Client->new
 ->get(q{http://news.ycombinator.com/news})
->success->dom->at(q{td.title a})->text;

Looking at the headers the website returns shows the culprit:

Content-Length: 0

Luckily, the same issue came up on the , and Sebastian offered a simple workaround to allow Mojo::Client to handle such a “broken” web server – this code works:


my $c = Mojo::Client->new;
my $tx = $c->build_tx(get => q{http://news.ycombinator.com/news});
$tx->res->content->relaxed(1);
$c->process($tx);
print $tx->res->dom->at(q{td.title a})->text;
Categories
Software and Programming

Here’s why people hate Perl, folks

I’ve been reading a bunch of Perl blogs recently, which seem to have seen a flurry of activity following Matt Trout’s call to Perl people to get blogging and spread the word. Tied into the urgency of Trout’s call is a common refrain of “why don’t programmers like Perl?” John Napiorkowski asks why all the hate? (the answer, it turns out, is because he looks sort of cross in his profile picture), David Golden thinks people should stop with the perl golf, and Sam Crawley thinks that people dislike Perl because they’ve had to deal with badly written Perl code, as does Gabor Szabo.

Actually, the image of someone coming to loath Perl after having to maintain a heap badly written code is a recurring idea in these posts and comments. And I guess that’s because it’s an accurate image. Because everyone who’s worked with Perl has had to face that pile of crappy old code, and either he’s cursed it and sworn never to touch the stuff again, or he’s said to himself, I can do this better! – and proceeded to write a fresh pile of clever Perl code, which he will find, a year or two down the road, has become to his horror another pile of ugly old code he is loath to maintain.

I’ve been programming Perl for a living for the past 9 years, and I’ve been dealt my share of heaps of bad code to maintain. I’ve also written some. And I came to realize, as I tried to scramble up their too-clever-for-my-own-good slopes that what I was climbing was someone else’s learning curve. And what I was leaving behind me was the detritus of my own education.

Perl is a complex language. By design. It’s filled with switches and toggles and shortcuts and magic, and a wondrous embedded mini-language of regular expressions, designed to make you feel sexually inadequate if you use four lines of code to parse a line of text instead of one. Conversely, Perl is missing some other pretty basic features which weren’t around when Larry was distilling it from the soul of Unix. Therefore it also has an elaborate set of workarounds and tricks to provide those missing features. People get beguiled into using all this stuff, and by using it learn how it can cut off their fingers: For example, why write ugly C-like || or && when you can write the more readable or or and? Well, because those operators have different precedence and are not interchangeable, and using the wrong one will bite you in the ass.

But getting bitten in the ass is how we learn; we learn new features by trying them out, getting past the obvious syntax and logic bugs needed to get our code to work, and letting the more subtle barbs slip by (Perl is marvelous at getting badly written code to Just Work). We call this DWIM – Do What I Mean – and while it is more useful, it’s no more trustworthy than a C++ compiler. To actually know what you’re doing with Perl, you need to see past DWIM to the subtleties, things like autovivification.

Another favorite Perl acronym is Perl’s motto, TMTOWTDI, There’s More Than One Way To Do It. CPAN is a magnificent edifice built to celebrate TMTOWTDI. Perl’s Object Oriented faculties are some syntactic sugar and a piece of string? Well, CPAN is overflowing with ways to create objects, classes, accessors, attributes, inheritance, prototypes, method exporters, roles, etc. etc. Testing frameworks? Templates? ORMs? Web frameworks? Other languages also have a selection of each, but how many of them include five subtly-different object models in each? Furthermore, Perl developers in CPAN seem to thrive on the novel: YAML! Source Filters! Prototype-based Inheritance, declarative syntax, cats and dogs living together, Attributes!

No wonder my boss likes to write (and has written) his own everything. But CPAN is not only a gaudy distraction, it’s an immensely useful resource, and for every non-trivial bit of code you want to do in Perl, you can bet anything that someone’s already done it, probably better than you could (simply because he’s cleaned his code enough to put it up online, where it’s possible for someone trying to use it for something that isn’t it’s original purpose to try it). There are many wonderful modules in CPAN, but learning about them, learning to use them, finding out about their hidden corner-cases and unexpected behaviors takes even more time, and the code you write while learning about CPAN modules is more code on the heap that someone will eventually need to maintain – which is particularly annoying if what you learn eventually is “I really shouldn’t have used that module for this”.

So, to summarize, Perl has by design a steep and scary learning curve. You can be productive in it pretty quickly – I see the sysadmins in the next office using it quite contentedly (it beats learning shell scripting, IMHO), but the code you write is supposed to mutate over time, as you learn and apply more of the language. Larry Wall often talks about Perl as equivalent to a natural language, and says how a five year old can use English properly, but you can’t expect a grown-up to speak like a five year old. In other words, most of the Perl code in the world, and even on CPAN, is written by pre-teens learning to express themselves.

This experience appeals to a certain mindset: programming Perl, you keep learning new things! And then there are new modules on CPAN which have some shiny New! Conceptual! Design! Because CPAN is written by people who have bought into the Perl mindset. This probably goes hand-in-hand with Perl’s “poor PR” – writing blog posts is boring, messing with RSS mashup frameworks that pull in half of CPAN is fun.

Steve Yegge’s Perl rant (a very insightful piece, and the more you know Perl, the more you’ll get out of it) makes the point (in passing) that Perl already has awesome hype and marketing, and that this marketing is aimed at directly at programmers. It’s clear to me that the reasons for Perl’s unpopularity are the same reasons it’s popular with the particular crowd that uses it. People hate Perl for the same reason we love it – because it makes us feel clever.