http://www.perlmonks.org?node_id=177534

I was just dinking around with Geo::WeatherNOAA, which does most of the work in this CGI. I just thought it was cool how easy it is to home-grow a weather forecast webpage. I am using the make_noaa_table function from the module, which returns formatted html--there are also functions that return in plain text so you can format as needed...

I use a file cache for results since it takes 5 seconds or so to return data from NOAA. If the cache is more than an hour old, it will retrieve the data and overwrite the cache. Otherwise, data just gets pulled from the file.

If you implement this CGI, make sure you add a "weather_cache" subdirectory of your cgi-bin directory and make it world-writeable.

#!/usr/bin/perl use strict; use Geo::WeatherNOAA; use CGI qw(param); my ($city, $state); if (param("city")) { $city = param("city"); } else { $city = "Minneapolis"; } if (param("state")) { $state = param("state"); } else { $state = "MN"; } print "Content-type: text/html\n\n"; print <<EOHTML; <HTML> <HEAD> <TITLE>NOAA Weather</title> </head> <BODY TEXT="#003366" BGCOLOR="#FFFFFF" ALINK="#0000FF" VLINK="#FF232D" +> <CENTER> <h1>Perl Weather Reporter</h1> <HR> EOHTML print "<form><table><tr><td align = right><b>City:</b></td> <td align = left><input type = text name = city></td></tr>\n"; print "<tr><td align = right><b>State:</b></td> <td align = left>"; print_state_select_menu(); print "</td></tr>\n"; print "<tr><td colspan = 2 align = center><input type = submit value = + \"Get Forecast\"></td></tr>\n"; print "</table></form>\n"; print "<h3><font color = red>Weather Forecast for</font> $city, $state +</h3>"; my $html_weather_table = get_weather_data($city,$state); print $html_weather_table; print "</body></html>\n"; exit; sub get_weather_data { my ($city, $state) = @_; my $filename = "weather_cache/${city}_${state}.out"; my ($return, $time, $mtime, $diff); if (-e $filename) { $time = time; $mtime = (stat("weather_cache/$filename"))[9]; $diff = $mtime - $time; } else { $diff = 0; } if ($diff > 3600 || !$diff) { # file older than an hour or doesn't +exist $return = make_noaa_table($city,$state); open(CACHE,">$filename") or warn "couldn't open weather_cac +he/$filename: $!"; print CACHE $return; close(CACHE); return $return; } else { open(CACHE,"<$filename") or warn "couldn't open weather_cac +he/$filename: $!"; { local $/ = "\0"; $return = <CACHE>; } close(CACHE); return $return; } } sub print_state_select_menu { print '<select name="state" > <option value=""> -- select one -- <option value="AL">Alabama <option value="AK">Alaska <option value="AZ">Arizona <option value="AR">Arkansas <option value="CA">California <option value="CO">Colorado <option value="CN">Connecticut <option value="DE">Delaware <option value="FL">Florida <option value="GA">Georgia <option value="HA">Hawaii <option value="ID">Idaho <option value="IL">Illinois <option value="IN">Indiana <option value="IO">Iowa <option value="KA">Kansas <option value="KY">Kentucky <option value="LA">Louisiana <option value="ME">Maine <option value="MD">Maryland <option value="MA">Massachusetts <option value="MI">Michigan <option value="MN">Minnesota <option value="MS">Mississippi <option value="MO">Missouri <option value="MT">Montana <option value="NE">Nebraska <option value="NV">Nevada <option value="NH">New Hampshire <option value="NJ">New Jersey <option value="NM">New Mexico <option value="NY">New York <option value="NC">North Carolina <option value="ND">North Dakota <option value="OH">Ohio <option value="OK">Oklahoma <option value="OR">Oregon <option value="PA">Pennsylvania <option value="RI">Rhode Island <option value="SC">South Carolina <option value="SD">South Dakota <option value="TN">Tenneessee <option value="TX">Texas <option value="UT">Utah <option value="VT">Vermont <option value="VA">Virginia <option value="WA">Washington <option value="WV">West Virginia <option value="WI">Wisconsin <option value="WY">Wyoming </select>'; }