Skip to content

Instantly share code, notes, and snippets.

@cblanc cblanc/postcodesio.pl
Last active Feb 9, 2019

Embed
What would you like to do?
Postcodes.io - Perl Example Postcode Lookup
#!/usr/bin/perl
# Kindly provided by github/@digdilem
my $pc = "TQ13 9XT"; # Example postcode (Dartmoor National Park Visitor's Centre, Haytor)
use LWP;
use JSON qw( decode_json );
my $ll_url = "https://api.postcodes.io/postcodes/$pc";
my $ua = LWP::UserAgent->new;
$ua->agent("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15");
my $req = HTTP::Request->new(GET => $ll_url);
my $response = $ua->request($req);
if ($response->is_success) {
my $json = decode_json( $response->content );
my $lat = $json->{'result'}{'latitude'};
my $lng = $json->{'result'}{'longitude'};
print "Lat/lang = $lat / $lng \n";
} else {
print "Failure in lookup \n"; return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.