5 # this has been taken from Geo::METAR
10 # fetch_temp.pl is a program that demonstrates how to get the current
11 # temperature from a nearby (or not) airport using Geo::METAR and the
14 # Given an airport site code on the command line, fetch_temp.pl
15 # fetches the current temperature and displays it on the
16 # command-line. For fun, here are some example airports:
23 # and of course: EGSH (Norwich)
28 my $site_code = uc shift @ARGV;
30 die "Usage: $0 <site_code>\n" unless $site_code;
32 # Get the modules we need.
38 my $ua = new LWP::UserAgent;
40 my $req = new HTTP::Request GET =>
41 "http://weather.noaa.gov/cgi-bin/mgetmetar.pl?cccc=$site_code";
43 my $response = $ua->request($req);
45 if (!$response->is_success) {
47 print $response->error_as_HTML;
48 my $err_msg = $response->error_as_HTML;
54 # Yep, get the data and find the METAR.
58 $data = $response->as_string; # grap response
59 $data =~ s/\n//go; # remove newlines
60 $data =~ m/($site_code\s\d+Z.*?)</go; # find the METAR string
61 my $metar = $1; # keep it
65 if (length($metar)<10) {
66 die "METAR is too short! Something went wrong.";
69 # pass the data to the METAR module.
72 print $m->as_string, "\n";