Postcodes.io - Perl Example Postcode Lookup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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