| File: | bin/geo-parse |
| Coverage: | 72.6% |
| line | stmt | bran | cond | sub | time | code |
|---|---|---|---|---|---|---|
| 1 | #!/usr/bin/env perl | |||||
| 2 | ||||||
| 3 | 1 1 1 | 1660 1 15 | use strict; | |||
| 4 | 1 1 1 | 1 0 17 | use warnings; | |||
| 5 | ||||||
| 6 | 1 1 1 | 152 3 13 | use Geo::Address::Parser; | |||
| 7 | 1 1 1 | 284 4306 1 | use Getopt::Long; | |||
| 8 | 1 1 1 | 249 17765 44 | use Pod::Usage; | |||
| 9 | 1 1 1 | 2 0 38137 | use JSON::MaybeXS; | |||
| 10 | ||||||
| 11 | 1 | 28166 | my $country; | |||
| 12 | my $help; | |||||
| 13 | 1 | 0 | my $man; | |||
| 14 | 1 | 0 | my $json_output; | |||
| 15 | ||||||
| 16 | 1 | 1 | GetOptions( | |||
| 17 | 'country|c=s' => \$country, | |||||
| 18 | 'help|h' => \$help, | |||||
| 19 | 'man' => \$man, | |||||
| 20 | 'json' => \$json_output, | |||||
| 21 | ) or pod2usage(2); | |||||
| 22 | ||||||
| 23 | 1 | 365 | pod2usage(1) if $help; | |||
| 24 | 1 | 1 | pod2usage(-verbose => 2) if $man; | |||
| 25 | ||||||
| 26 | 1 | 2 | my $input = join(' ', @ARGV); | |||
| 27 | 1 | 1 | if (!$input) { | |||
| 28 | 0 | 0 | print STDERR "No address given.\n"; | |||
| 29 | 0 | 0 | pod2usage(1); | |||
| 30 | } | |||||
| 31 | ||||||
| 32 | 1 | 1 | if (!$country) { | |||
| 33 | 0 | 0 | print STDERR "No country given.\n"; | |||
| 34 | 0 | 0 | pod2usage(1); | |||
| 35 | } | |||||
| 36 | ||||||
| 37 | 1 | 2 | my $parser = Geo::Address::Parser->new(country => $country); | |||
| 38 | 1 | 2 | my $result = $parser->parse($input); | |||
| 39 | ||||||
| 40 | 1 | 66 | if (!$result) { | |||
| 41 | 0 | 0 | print STDERR "Failed to parse address.\n"; | |||
| 42 | 0 | 0 | exit 1; | |||
| 43 | } | |||||
| 44 | ||||||
| 45 | 1 | 1 | if ($json_output) { | |||
| 46 | 1 | 21 | print JSON::MaybeXS->new->canonical->pretty->encode($result); | |||
| 47 | } else { | |||||
| 48 | 0 | 0 | foreach my $field (qw(name street suburb city region postcode country)) { | |||
| 49 | 0 | 0 | printf "%-10s: %s\n", ucfirst($field), $result->{$field} // ''; | |||
| 50 | } | |||||
| 51 | } | |||||
| 52 | ||||||
| 53 | ||||||
| 54 - 76 | =head1 NAME geo-parse - Command-line tool for Geo::Address::Parser =head1 SYNOPSIS geo-parse --country US "1600 Amphitheatre Pkwy, Mountain View, CA 94043" geo-parse -c NZ "Auckland Museum, 1 Museum Circuit, Parnell, Auckland 1010" geo-parse --help geo-parse --man =head1 OPTIONS -c, --country Country code (e.g., US, UK, CA, AU, NZ) -h, --help Print a brief help message --man Full documentation -j, --json JSON output =head1 DESCRIPTION This script parses a freeform postal address and extracts structured fields using Geo::Address::Parser. | |||||