PreshBlog

Why I love Perl : It makes things easy and fun

by on Nov.08, 2011, under Uncategorized

A short example of why I love Perl. The other day, I was picking my parents up from the airport; I do this reasonably regularly, as they go to visit family out in Ireland. Anyway, I’ve often wanted something to monitor the status of the incoming flight on Luton Airport’s website and tell me when it changes (if it’s delayed and a new estimated time is announced, or when it’s landed, etc). I looked around for decent services which do this, but found nothing which really appealed.

So, the other day, I decided “write it yourself, it’ll only take a few minutes”. Sure enough, with the aid of LWP::Simple, HTML::TableExtract, SMS::AQL and a little glue code, it was done:

#!/usr/bin/perl
 
use strict;
use LWP::Simple;
use HTML::TableExtract;
use SMS::AQL;
 
my $flight = shift or die "Usage: $0 flightnum";
my $url ="http://www.london-luton.co.uk/FlightData.ashx"
    . "?dir=arr&lang=en&id=1&r=20016807";
my $dest = '+44******';
 
my $sms = new SMS::AQL({
    username => '******',
    password => '******',
    options => { sender => '+44*****' },
});
 
my $last_status;
check:
while (1) {
    sleep 30 if $last_status;
    my $html = LWP::Simple::get($url) or warn "Failed to fetch HTML" and next;
    my $te = HTML::TableExtract->new(
        headers => [ 'Flight No', 'Airport', 'Scheduled', 'Flight Status' ]
    );
    $te->parse($html) or warn "Failed to parse HTML" and next;
 
    for my $row ($te->rows) {
        if ($row->[0] eq $flight) {
            if ($row->[3] ne $last_status) {
                $sms->send_sms($dest, "Flight $flight now $row->[3]");
            }
            $last_status = $row->[3];
 
            next check;
        }
    }
}

How easy was that?

(The code isn’t quite up to the quality I’d aim for if I was releasing it as an actual project – I’d tidy it up a bit, add more error checking/handling and have it read details from a conf file if I were to do that, but it’s a good example of what you can quickly rock up with the power of Perl and CPAN. I’d probably also swap it to SMS::Send, so it could be more easily used with various SMS gateways (I use www.aql.com – there’s an SMS::Send::AQL driver I wrote for that, which uses SMS::AQL under the hood anyway).)


2 Comments for this entry

  • John

    You have a great talent, how can this take you only a few minutes? So, wish i could do it…

  • Aventurine

    Perl is not that pretty,i recommend python. You’ll fall in love with your program. Although, that program of yours is quite neat(can’t think of a way to do it in python). :)

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!