Skip to content

Instantly share code, notes, and snippets.

@cblanc
Last active February 9, 2019 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cblanc/ecb2c3aec4a8759f1fba19c98411f39c to your computer and use it in GitHub Desktop.
Save cblanc/ecb2c3aec4a8759f1fba19c98411f39c to your computer and use it in GitHub Desktop.
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