Uses the zip code finder at www.usps.gov to match a zip code to a city, or a city to a zip code. It's simple and short, but I learned about LWP::UserAgent and how exactly \G works in a regexp.
#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; my $USPS = 'http://www.usps.com/cgi-bin/zip4/ctystzip2'; my $FormDataID = 'ctystzip'; my ($x, @matches); if (@ARGV) { $x = join " ", @ARGV; } else { print 'Enter Search (zip code OR city, state): '; chomp( $x = <STDIN> ); } # Prepare data to send to the USPS my $ua = new LWP::UserAgent; my $req = new HTTP::Request POST => $USPS; $req->content_type('application/x-www-form-urlencoded'); $req->content($FormDataID.'='.$x); # HTTP::Request handles URL encodin +g. # Request the page, and ignore initial content my $res = $ua->request($req); my $data = $res->content; $data =~ /----------</g; # If a zip code was submitted, we are looking for CITY STATE if ($x =~ /^\s*\d{5}\s*/) { while ($data =~ /\G.*?br>\s*((\w+\s)+\s*\w\w) /ig) { push @matches, $1; } @matches = map { $_ = reverse $_; /^\s*(\w\w)\s*(\w.*)$/; scalar reverse "$1 ,$2"; } @matches; } # Otherwise, Extract the zip codes. else { while ($data =~ /\G.*?br>\s*(\d{5})/ig) { push @matches, $1; } for (@matches) { s/^\s+//; s/\s+$//; } } print 'Found ',$#matches+1," matches for $x\n ", join("\n ", @matches),"\n"; __END__ =head1 NAME zipcode.pl - City, State to/from Zip Code converter =head1 SYNOPSIS perl zipcode.pl [City, State] or perl zipcode.pl [Zip Code] =head1 AUTHOR Dean Serenevy <dean@serenevy.net> =cut

In reply to City, State to/from Zip Code converter by duelafn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.