Single post
Finding the distance between postcodes
A recent project called on me to refresh my experience of working in perl. The task was to find the distance between two UK postcodes using Object-oriented Programming (OOP).
The following three files make up the code:
postcode.pl
postcode.pm
postcodes.csv
The perl script, postcode.pl
takes input of two postcodes (spaces aren’t important either within the postcodes or between as I’ve used a regular expression to match them from the given command-line arguments) and returns the distance between the two in kilometers.
In this case I have used the haversine law, in contrast to my previous work in Visual Basic for Applications, which due to a rounding error in excel meant I had to drop back to Pythagoras’ Law on Equiangular Projections
The usage is:
my $PostCode = new postcode(string $postcode1);
my $distance = $PostCode->distanceTo(string $postcode2);