Skip to content

Instantly share code, notes, and snippets.

@mtimkovich
Last active December 11, 2015 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtimkovich/4528798 to your computer and use it in GitHub Desktop.
Save mtimkovich/4528798 to your computer and use it in GitHub Desktop.
Program to fetch the weather information
#!/usr/bin/perl
use strict;
use warnings;
use feature qw(switch say);
use Getopt::Std;
use LWP::Simple;
sub help {
say "Usage: $0 [-cf] <zip code>";
exit;
}
my $metric = 0;
my %opts;
help if not getopts("cf", \%opts);
if (not @ARGV) {
help;
}
if ($opts{c}) {
$metric = 1;
}
if ($opts{f}) {
$metric = 0;
}
my $loc = $ARGV[0];
my $sign = $metric ? "'C" : "'F";
my $url = "http://rss.accuweather.com/rss/liveweather_rss.asp?metric=$metric&locCode=$loc";
my $xml = get $url;
if (not defined $xml) {
say "$sign";
exit;
}
my @lines = split /\n/, $xml;
if (grep /Invalid Location/, @lines) {
die "'$loc' is not a valid location\n";
}
my $weather = (grep /Currently/, @lines)[0];
($weather) = $weather =~ /(\d+)/;
say "$weather$sign";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment