#!/usr/bin/env perl # weather_watch.pl use strict; use Getopt::Long; use JSON::XS; use LWP::Simple; use Sys::Syslog; my $app = bless { city => 'London,uk', }; GetOptions( $app, 'city=s', ); $app->init; $app->run; sub init { my ($app) = @_; openlog($0, 'pid', 'user'); syslog("info", "Starting up"); $SIG{TERM} = sub { $app->{should_stop} = 1; }; } sub run { my ($app) = @_; my $url = "http://api.openweathermap.org/data/2.5/weather?q=$app->{city}"; until ( $app->{should_stop} ) { if ( my $json = get($url) ) { my $data = decode_json($json); syslog("info", "Temperature is $data->{main}{temp}"); syslog("info", "Wind speed is $data->{wind}{speed}"); } sleep 5; } syslog("info", "Shutting down"); closelog(); }