pw2kindle: a perl projet meant to experiment

10 March 2012, 14:03

Intro

I love my kindle. With it, I’ve been reading even more than before. Especially during commuting. I read books at first but then discovered instapaper which allows you to push articles in a ‘to read’ list which then gets converted into a magazine format and wirelessly delivered to your kindle for free! This is so convenient that it went to the point where I’ve mostly stopped reading in front of the computer.

I love perl! I love reading about perl! As a “born-again” perl developer I’ve stumbled on perlsphere and have been browsing it more or less thoroughly since 2-3 years. I say more or less thoroughly due to the fact that I have to remember to come back to it from time to time. Then, lately, Perl Weekly arrived into our lives, and I thought: “If I could read that on my kindle it would be awesome!”.

Enter pw2kindle.

First, I must say that my next from-scratch perl project had to incorporate new and fancy things as a test bed to simplify stuff at work. pw2kindle is that next project. What does this means? This software is completely overkill and out of proportion for such a simple task.

Here’s a quick rundown of the stuff I tried and the stuff I learned:

Command line application framework

I wanted to play with CLI framework. First and foremost because a simple perl script isn’t testable until you factor it out into a module. But mainly because, at work, we have probably one of the worst case of a command-line application that I would like to split into chunks and refactor (but never got the time to… you know, the reality of a small-business doing open source…).

So I wanted to experiment with command line applications frameworks. After some research:

Were the candidates. In the end it came down to MooseX::App::Cmd versus CLI::Framework. I chose MooseX::App::Cmd because of Moose. I have always wanted to try Moose out on a ‘real’ problem (instead of doing tutorials). Although I have to give a mention for CLI::Framework (CLIF) which seems definitely the more powerful one and then one I would’ve gone with if it was not for Moose.

Web::Query

Another great thing I played with is Web::Query. To quote the author’s description:

Web::Query is a yet another scraping framework, have a jQuery like interaface.

Look at the kind of terseness you can achieve with it. The following adds $article objects to the @articles array that come from all <p class=entry><a class=...>title</a><p>description</p></p> in the given URL:

my @articles;
wq("perlweekly.com/archive/$issue.html")
    ->find('p.entry>a')
    ->each(sub {
        my $i = shift;
        my $article = Pw2Kindle::Model::Article->new(
            title => $_->text,
            url => $_->attr('href'),
        );  

        printf("%d) %s\n", $i+1, $article->toString()) if $self->dryrun();
        push @articles, $article
});

The beauty is in the find() with it’s CSS style selector (borrowed from jQuery I guess?) and then the ability to run an each with a closure on it. Very neat!

Problems along the way

The frame that App::Cmd provides needs some effort learning but I also see a lot of benefit in the boiler plate removal. It’s doing all the splitting for you, enforcing some structure in the package naming. Also, it recommends using autouse so large command-line applications probably benefit from it performance wise.

That said, I spent some time in the debugger tracing problems blaming issues on Moose although it turned out that it was a user problem.. To my defense, the documentation doesn’t covers unit testing.. So I spent some time thinking it was a MooseX::App::Cmd to App::Cmd mismatch problem instead of me misusing the framework trying to test.. In any case, I traced it and fixed it, it was my fault using new() instead of prepare().

What is pw2kindle

Right now, being mostly focused on getting Perl Weekly on my kindle quickly, here’s what the thing does:

What should be pw2kindle

Ideally I would like to do the ‘instapaper’ text (and relevant image) grinding myself but I have another project that needs attention right now (shameless plug: Hacker Jeopardy @ HackUS 2012) so I won’t touch it for little while.

Concluding

The Moose learning was awesome! I love that stuff although I barely used it. I enjoyed the structure and splitting provided by the CLI framework. I’m definitely re-using one of these on any relatively large command line application. I hope I will have the chance to make this one grow.

Now, I can’t wait read Perl Weekly on my kindle.

Feel free to fork pw2kindle! Check out the TODO if you are interested to contribute.

— Olivier Bilodeau

spacer

Comment

Textile Help

<-   ->

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.