Perl checks for rain and issues umbrella warning

Umbrella or Bust?

To determine whether it will rain on any given day on your way to and from work and, thus, whether you need an umbrella, you can launch the script in Listing 2 at dawn as a cronjob. In line 12, it defines the critical hours – 8 and 9am for commuting to work and 5, 6, or 7pm for commuting back home after work.

Listing 2


01 #!/usr/local/bin/perl -w
02 use strict;
03 use WWW::Wunderground::API;
04 use DateTime;
05 use Log::Log4perl qw(:easy);
06 use Getopt::Std;
08 getopts "v", \my %opts;
09 Log::Log4perl->easy_init(
10   $opts{ v } ? $DEBUG : $ERROR);
12 my @critical_hours = qw(8 9 17 18 19);
14 my $w = WWW::Wunderground::API->new(
15   location => "San Francisco, CA",
16   api_key  => "0123456789abcdef",
17   auto_api => 1,
18 );
20 for my $e (@{ $w->hourly }) {
21   if( !scalar @critical_hours ) {
22     last;
23   }
25   my $dt = DateTime->from_epoch(
26     epoch => $e->{ FCTTIME }->{ epoch } );
28   if( $dt->hour() == $critical_hours[0] ) {
29     DEBUG "Checking hour ", $dt->hour(),
30       " ($e->{ pop }%)";
32     if( $e->{ pop } > 50 ) {
33       printf "Take your Umbrella! " .
34         "(%d%% rain at %s)\n",
35           $e->{ pop }, $dt->hms();
36     }
38     shift @critical_hours;
39   }
40 }

In line 15, you need to define the weather location in the location parameter and again the access key in api_key. The auto_api option ensures that the CPAN module calls the Wunderground server's REST interface behind the scenes and returns the desired JSON array using the hourly() method invoked in line 20.

When called without any parameters, the script only prints a message if the probability of rain in the critical hours is more than 50 percent. Then the printf statement in line 33 prints a string such as

Take your Umbrella! (52% rain at 19:00:00)

to standard output, and the subsequent email script in Listing 3 sends the message to the eagerly waiting users.

Listing 3


01 #!/usr/local/bin/perl -w
02 use strict;
03 use warnings;
04 use LWP::UserAgent;
05 use JSON qw(to_json);
07 my $text = join "", <>;
09 if( !length $text ) {
10   exit 0;
11 }
13 my $api_key = "abcdefghi0123456789";
14 my $from    = '';
15 my $to      = '';
17 my $ua = LWP::UserAgent->new();
19 my $resp = $ua->post(
20   "" .
21     "messages/send.json",
22   Content => to_json( {
23     key => $api_key,
24     message => {
25       text    => $text,
26       subject => $text,
27       from_email => $from,
28       to => [ { email => $to } ],
29     } } ),
30 );
32 if( $resp->is_error() ) {
33   die "Sending mail failed: ",
34     $resp->message();
35 }

If there is no danger of rainfall drenching the employee, the script does not output anything, and the subsequent emailer cancels without sending a message.

If you want to know exactly what the script is getting up to, call it with the -v option, as shown in Figure 5, and you will receive rain forecasts for the critical hours in the form of Log4perl messages. However, the cronjob needs to run the script without -v – like this, for example, at seven o'clock in the morning:

0 7 * * * umbrella | mandrill-email

to prevent the mailer from sending a message even if it does not look like rain. Note also that either the scripts must be in a directory that the cronjob can find or the cron entry must explicitly specify the absolute path to the scripts.

Figure 5: In verbose mode, the script tells you which messages from the weather service it is checking.

Looking for a Mail Provider

In this age of spam, it isn't easy to automate the process of sending email. At the office, or at home, you can do this because the ISP can verify who is doing what. To be flexible, it is a good idea to turn to a third party. For example, Mandrill allows you to use their email servers for a fee, but test accounts can also send a few email messages for free.

Email with REST

Interested users can register with a valid email address [4] and then receive a 22-digit API key in Base64 format in turn, which the script can later use to send mail via Mandrill's REST server. However, note at this point that the From address when sending email must always be the valid email address you specified when registering. If this is not the case, then Mandrill refuses to provide the service, at least for the free trial accounts.

Line 7 in Listing 3 [5] grabs the text data arriving at the standard input and stores it in $text. If this string is empty because the previous script did not output anything to the Unix pipeline, line 10 terminates without an error message. In this case, nothing would be sent. In line 13, replace $api_key with your Mandrill test key and the sender address in $from (line 14) with the email you used to register. You can specify the receiving address to suit your needs.

The script uses the to_json function from the JSON module on CPAN to encode the email data and then sends it to the Mandrill server via a POST request. If line 32 does not detect any errors, the email was sent, and you can check your smartphone before you rush off to work to see whether the weather service advises taking the risk or taking an umbrella just in case.


  1. Weather Underground:
  2. Weather Underground, developer API:
  3. Probability of precipitation:
  4. Mandrill email service
  5. Listings for this article:

The Author

Mike Schilli works as a software engineer with Yahoo! in Sunnyvale, California. He can be contacted at Mike's homepage can be found at

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More