#!/usr/bin/perl use strict; use Geo::WeatherNOAA; use CGI qw(param); my $OK_CHARS = "-a-zA-Z0-9_.@'\"\s"; foreach (param()) { # sanitize data input from form if (param($_) =~ /[^$OK_CHARS]/) { print "Content-type: text/html\n\n"; print "Form ErrorWarning: One of the characters in your form entry is not allowed by this program. Characters that are allowed are A-Za-z0-9.@-\"'. Please hit back on your browser and try again."; exit(0); } } 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 < BenWorld


EOHTML print "

Weather Forecast for $city, $state

"; my $html_weather_table = get_weather_data($city,$state); print $html_weather_table; print "

\n"; print "

\n"; print "\n"; print "\n"; print "
Get weather for another City/State
City:
State: "; print_state_select_menu(); print "
\n"; print "\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 = $time - $mtime; } 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_cache/$filename: $!"; print CACHE $return; close(CACHE); return $return; } else { open(CACHE,"<$filename") or warn "couldn't open weather_cache/$filename: $!"; { local $/ = "\0"; $return = ; } close(CACHE); return $return; } } sub print_state_select_menu { print ''; }