The Current UK Population In JavaScript

One interesting webpage going round the office yesterday were the UK Population Statistics.

Looking at the figures for 2002-2003 we saw the total UK population grow from 59,321,700 people to 59,553,800 people.

That means the population grows by approximately 1 person every 2 and a half minutes.

I’ve knocked together a quick JavaScript that can calculate the approximate population of the UK assuming this increase is constant.

We work out the per minute increase in population. We also know the starting population of the UK on the 1st January 2003, all we have to do is work out how minutes have elapsed between then and now, and multiply that by the population growth per minute.

var startDate = new Date("January 1, 2003 00:00:00");
var perMinuteIncrease = 232100 / (365 * 24 * 60);
var startPop = 59553800;
// return the estimated current population of the UK.
function currentPopulation() {
var currentDate = new Date();
var diffMinutes = Math.floor((currentDate.getTime() - startDate.getTime()) / 60000);
return Math.round(startPop + (diffMinutes * perMinuteIncrease));
}

To get the current population you just call the currentPopulation() function.

Let’s see what the approximate current UK population is now according to this script…



Just reload/refresh the page to get an updated population count.

Querying RDF In Perl With RDFStore

Apart from RDF::Core and Redland, another option for parsing and querying RDF in Perl is RDFStore. This also provides the Perl RDQL::Parser module used by the very useful DBD::RDFStore driver.

Following on from the previous examples showing how to extract information from my FOAF file using RDF::Core (Query RDF In Perl With RDF::Core) and RDF::Redland (Querying RDF In Perl With RDF::Redland), here I’ll re-implement the query using RDFStore.

As a quick recap from the previous articles, here is the bit of RDF we want to extract information from.

<foaf:knows>
<foaf:Person>
<foaf:nick>Cal</foaf:nick>
<foaf:name>Cal Henderson</foaf:name>
<foaf:mbox_sha1sum>2971b1c2fd1d4f0e8f99c167cd85d522a614b07b</foaf:mbox_sha1sum>
<rdfs:seeAlso rdf:resource="http://www.iamcal.com/foaf.xml"/>
</foaf:Person>
</foaf:knows>

The solution used to extract the data from the RDF looks a lot more Perl-like than the previous examples we have seen.

If you have ever queried databases using SQL in Perl, then you have certainly come across the powerful DBI module. This abstracts the common database usage making it possible to very easily port your applications between various databases. One of the best things about using RDFStore is that it provides a DBD driver allowing you to use standard DBI methods when querying your RDF data. Unlike other modules that make you create triple stores and factory methods, RDFStore lets that be hidden from you.

To start with we’ll need to create a database handle using DBI and the DBD::RDFStore modules and store it in the variable $dbh.

my $dbh = DBI->connect("DBI:RDFStore:");

This creates a database on the fly, but we can connect to an existing database on a local or remote server if we so wished.

Now we need to create our RDQL query. It looks very similar to the query we used in the Redland example.

my $query = $dbh->prepare(<<QUERY);
SELECT ?name ?nick ?seeAlso ?mbox_sha1sum
FROM <file:foaf.rdf>
WHERE
(?x <rdf:type> <foaf:Person>),
(?x <foaf:name> ?name)
(?x <foaf:nick> ?nick)
(?x <rdfs:seeAlso> ?seeAlso)
(?x <foaf:mbox_sha1sum> ?mbox_sha1sum)
AND
(?nick eq 'Cal')
USING
foaf for <http://xmlns.com/foaf/0.1/>,
QUERY

Here we’re selecting the values the name, nick, seeAlso and mbox_sha1sum triples for a Person with the nick of Cal. We’ve explicitly set where our triples come from using the FROM clause. In this case, it’s the file foaf.rdf, which contains my FOAF information.

We have the query in the variable $query, so lets execute it.

$query->execute();

We can use standard DBI methods to fetch the data from our query. Here I’m going to create some bound variables to keep any matching data in.

my ($name, $seeAlso, $mbox_sha1sum, $nick);
$query->bind_columns($name, $nick, $seeAlso, $mbox_sha1sum);

Now we just have to fetch each row that matches our query and print them out.

while ($query->fetch()) {
print $name->toString, "n";
print $nick->toString, "n";
print $seeAlso->toString, "n";
print $mbox_sha1sum->toString, "n";
}

The values returned are either RDFStore::Literal or RDFStore::Resource objects, so we have to use their toString methods to print them.

To tidy up, we’ll finish our query and disconnect from our database.

$query->finish;
$dbh->disconnect;

That’s it! It really is as simple as that.

Let’s put this all together now to produce our final example code listing.

#!/usr/bin/perl -w
## An example showing how to use RDFStore and RDQL::Parser to
## extract information from a FOAF file.
## Copyright 2004 - Robert Price - http://www.robertprice.co.uk/
use strict;
use DBI;
## create a DBI connection to our NodeFactory.
my $dbh = DBI->connect("DBI:RDFStore:");
## prepare our query.
my $query = $dbh->prepare(<<QUERY);
SELECT ?name ?nick ?seeAlso ?mbox_sha1sum
FROM <file:foaf.rdf>
WHERE
(?x <rdf:type> <foaf:Person>),
(?x <foaf:name> ?name)
(?x <foaf:nick> ?nick)
(?x <rdfs:seeAlso> ?seeAlso)
(?x <foaf:mbox_sha1sum> ?mbox_sha1sum)
AND
(?nick eq 'Cal')
USING
foaf for <http://xmlns.com/foaf/0.1/>,
QUERY
## execute the query.
$query->execute();
## define some holding variables and bind them to our query results.
my ($name, $seeAlso, $mbox_sha1sum, $nick);
$query->bind_columns($name, $nick, $seeAlso, $mbox_sha1sum);
## while we have results being returned...
while ($query->fetch()) {
## print out the values.
## As these can be RDFStore::Literal or RDFStore::Resource's we
## need to use the toString method of these objects to print.
print $name->toString, "n";
print $nick->toString, "n";
print $seeAlso->toString, "n";
print $mbox_sha1sum->toString, "n";
}
## end the query and disconnect.
$query->finish;
$dbh->disconnect;

In conclusion, RDFStore provides a very clean and Perlish interface to querying RDF data. The code implements a DBD module allowing standard DBI methods to be used, making it quick and simple for Perl developers to learn and use effectively.

Serving MS Excel Documents From A Perl CGI Script

How can you serve a Microsoft Excel spreadsheet from a Perl CGI script?

It’s actually quite easy, and just a case of sending the right headers.

Assuming we have our raw binary Excel sheet in a variable called $excel, we can use this simple block of code to allow it to be downloaded from a web script with the filename of text.xls.

print "content-type: application/vnd.ms-exceln";
print "content-disposition: attachment; filename=text.xlsnn";
print $excel;

This will prompt the user with a file download box asking where to store text.xls.

The key here is the content-disposition tag. We could change the content-disposition to inline to try to force the browser to open the Excel document in browser itself, but that’s not really very friendly.