Category Archives: Perl

Perl, the duct tape of the Internet. Posts about Perl development, releases of my CPAN modules, etc.

pQuery – jQuery-like fun in Perl

I discovered pQuery the other day, and had been meaning to try it out.

I got the chance to use it today, and I have to say, I’m impressed. I am, however, rather wary that the last release was nearly two years ago; I’m not sure I’d want to use it in production code unless it’s being actively maintained – I might use it for non-important things before making that call.

Anyway, a brief example:

Let’s say you want to capture the content of a paragraph/div with the class “bar” from a page somewhere.. it can be as simple as:

pQuery('http://www.example.com/foo.html')->find('.bar')->html;

Let’s say you want to extract all H1 headings instead:

pQuery($url)
    ->find('h1')
    ->each(
        sub { say "Heading: " . pQuery($_)->text; }
    );

I’d love to see this continue in active development – there’s several features it could really use, but what’s already there is handy stuff!

Dancer::Plugin::Database – easy DB connections for Dancer apps

Last night I finished and released the first version of Dancer::Plugin::Database, a plugin for the Dancer web framework to provide easy database connections using the venerable DBI.

It takes the database connection details from the app config file, and provides a database keyword which will return you a connected database handle (taking care of ensuring that the DB connection is still alive, etc).

So, usage can be as simple as:

my $books = database->selectall_arrayref(
    'select * from books where author = ?', 
    { Slice => {}}, $author
);

(Calling database() simply returns a DBI database handle, so you can obviously do anything you can do with DBI).

Also, at the moment, if a database connection could not be established for any reason, database() will just return undef, so you will need to handle errors appropriately. After considering whether it’s wise, I may tweak it to die, so that Dancer will handle the failure with a pretty 500 error for you (in which case, I’ll make it possible to disable that via the config).

Several Perl scripts released to Github

I’ve been playing with Github lately, and rather liking it.

I did briefly try it about a year ago, and was impressed, but wasn’t driven enough to consider moving away from Subversion.

I tried it out more when I started contributing to the Dancer Perl framework, and got to like it; since then, I’ve moved several of my modules over to Github already.

I finally got round to moving some miscellanous Perl scripts too, ones which I’ve been meaning to release as open-source in case they’re of use to anyone, but didn’t get round to doing.

They’re now online at: http://github.com/bigpresh/misc-scripts.

Github made sharing my code easy – Github++;

Now, that’s enough geekery for one evening.

Dancer 1.150 released – a flexible, lightweight web framework for Perl

Version 1.150 of the Dancer web framework has just been released, so this seemed like a good time to write up this post.

Recently, I’ve been wanting to find a Perl web framework that I really got on with. I’ve used Catalyst, which is very powerful and popular, but it’s quite heavy (a lot of dependencies, and reasonably high memory usage and startup time), and I felt as though it forced me to code “the Catalyst way”, rather than staying out of my way and getting on with writing my code.

I took a look around at the current Perl web frameworks (e.g. CGI-Application, Jifty, Catalyst, Mojo…) – all good in their own ways, but for various reasons, none of them really struck me as something I’d be particularly happy to work with.

I did briefly consider trying to write my own, but that’s a wheel I do not want to re-invent – there’s enough odd-shaped wheels out there already.

When I found Dancer (a port of Ruby’s Sinatra framework), I immediately liked the fact it looked simple and stays out of the way as much as possible, so I gave it a try – and, I must say, I’m impressed.
Continue reading Dancer 1.150 released – a flexible, lightweight web framework for Perl

OpenDNS vs Google – speed comparison

I read a Twitter post earlier mentioning Google’s public DNS service, and suggesting that it could displace the popular OpenDNS

I thought it would be interesting to do a performance comparison between Google and OpenDNS, to see how they compare. I also decided to include the nameservers of my ISP, Virgin Media, to illustrate whether there are performance gains to be had by changing to OpenDNS (which I primarily use, along with others) or Google, or whether staying with defaults works. Continue reading OpenDNS vs Google – speed comparison

Configure CPAN.pm to use sudo to install

I prefer to run CPAN.pm as a normal user and have it use sudo just for the actual installation, rather than running tests etc as root.

I have an annoying habit of forgetting the option names , so I’m posting this here for my reference, but might be useful for other people too.

To do that, do the following from a CPAN shell:


o conf make_install_make_command 'sudo make'
o conf mbuild_install_build_command 'sudo ./Build'
o conf commit

Also, whilst documenting that, to set your preferred CPAN mirror:


o conf urllist unshift ftp://mirrors.uk2.net/pub/CPAN/
o conf commit

Subversion – show commit details when editing commit message

Something I’ve wanted to do for a while is get the list of changed files and a diff into the commit message in my editor when I make a commit with Subversion.

With Git, you can pass the -v (verbose) option when committing, and the commit message you edit will include diffs as well as the list of modified files.

Subversion provides no such option, so I put together a little wrapper shell script to do this for me.

The script provides a function named svncommit (which I alias to just ‘ci’ for supreme shortness :) ).

When used, after the “–This line, and those below, will be ignored–” marker line, the list of files and then diffs will be inserted, as shown in the screenshot below (click for full size):

Subversion commit message being edited

The script itself is relatively simple (it was knocked up quickly; I’ll probably improve on it sometime):

# Do an svn commit, with diffs included in the commit message
svncommit() {

    # Start preparing the commit message which we'll then edit
    COMMITMSG=/tmp/$USER-commitmsg
    echo > $COMMITMSG
    echo "--This line, and those below, will be ignored--" >> $COMMITMSG
    svn status "$@" >> $COMMITMSG
    echo >> $COMMITMSG

    # Now do a diff; work out stats on lines added/removed by looking at
    # the diff, add that info, then the diff itself
    svn diff "$@"   > /tmp/$USER-svndiff
    LINESADDED=$(  grep '^+[^+]' /tmp/$USER-svndiff | wc -l)
    LINESREMOVED=$(grep '^-[^-]' /tmp/$USER-svndiff | wc -l)
    echo "Added $LINESADDED lines, removed $LINESREMOVED lines" >> $COMMITMSG
    echo >> $COMMITMSG
    cat /tmp/$USER-svndiff >> $COMMITMSG
    echo >> $COMMITMSG

    ORIGMD5=$(md5sum $COMMITMSG)
    $VISUAL $COMMITMSG

    if [[ "$(md5sum $COMMITMSG)" == "$ORIGMD5" ]]; then
        echo "Commit message unchanged, commit aborted";
    else
        svn commit "$@" -F $COMMITMSG
    fi

    rm $COMMITMSG
    rm /tmp/$USER-svndiff
}

Retagging audio tracks based on filename

I had some audio tracks which weren’t tagged, but did have filenames containing the artist, title etc, so I whipped up a quick Perl script to sort them out – retag-by-filename.pl.

It takes a regular expression with named captures for track, title, artist and comment, and sets the tgs on the file as appropriate.

It makes use of Music::Tag to do the actual tagging and Getopt::Lucid to read the options supplied, and requires Perl 5.10.0 for named regex captures (and ‘say’).

A --dry-run option allows you to check that the filenames are being parsed correctly by your regex before actually writing tags.

See retag-by-filename.pl for the full details.