use strict; use warnings; use Geo::WeatherNOAA; use MIME::Lite; my ( $city, $state, $email, $mailhost ) = @ARGV; $city = $city || 'Los Angeles'; $state = $state || 'CA'; if ( $^O =~ /MSWin32/ and defined $mailhost ) { MIME::Lite->send( 'smtp', $mailhost, Timeout=>60 ) } { my %abbrev = map { chomp; split /\s*\|\s*/ } ; my $keywords = join '|', keys %abbrev; sub abbreviate { my ( $prefix, $text ) = @_; my $out = lc ( $prefix . ': ' . $text ); $out =~ s/\b($keywords)\b/$abbrev{$1}/g; # Abbreviate $out =~ s/\.{3}/./; # Repunctuate $out =~ s/\s\bin\b//g; # drop 'in' $out =~ s/\s\bthe\b//g; # drop 'the' $out =~ s/\s*([,.;\/]+)\s*/$1 /g; # Too much space. return $out; } } my ( $date, $warnings, $forecast, $coverage ) = process_city_zone( $city , $state, '', 'get'); foreach my $warning ( @$warnings ) { if ( $warning =~ /\w/ ) { my $out = abbreviate ( '!!!', $warning ); if ( defined $email ) { my $msg = MIME::Lite->new( 'From' => $email, 'To' => $email, 'Subject' => 'Warning', 'Data' => $out ); $msg->send; } else { print "Warning:\n$out\n"; } } } foreach my $frame ( @{ prepare_forecast( $forecast ) } ) { if ( defined $email ) { my $msg = MIME::Lite->new( 'From' => $email, 'To' => $email, 'Subject' => 'Forecast', 'Data' => $frame ); $msg->send; } else { print "$frame\n"; } } sub prepare_forecast { my ( $info ) = shift; return [ map { abbreviate( $_, $info->{$_} ) } keys %$info ]; } __DATA__ tonight| tnite patchy| ptch clear| clr evening| eve cloudy| cldy overnight| ovrngt upper| upr morning| am afternoon| pm south| S north| N east| E west| W northeast| NE southeast| SE southwest| SW northwest| NW winds| wind monday| Mon tuesday| Tue wednesday| Wed thursday| Thu friday| Fri saturday| Sat sunday| Sun tomorrow| Tomrw through| - otherwise| othrwse then| , night| nite inland| inlnd beaches| beach lower| lwr and| / around| arnd