#!/usr/bin/perl use strict; use Geo::Weather; exit(main(@ARGV)); sub main { my $city; my $state; my $zip; while (my $arg = shift){ if ($arg eq '-s'){ $state = shift; } elsif ( $arg eq '-c' ){ $city = shift; } elsif ( $arg eq '-z' ){ $zip = shift; } else { return usage('Unknown argument'); } } return usage('Insufficent arguements') unless ( ( $city and $state ) or $zip ); return usage('Supply zip or city, not both.') if $city and $zip; my $weather = new Geo::Weather; $weather->{timeout}; $weather->get_weather($zip) if $zip; $weather->get_weather($city, $state) if $city; n3ofWeather($weather); } sub n3ofWeather { my $weather = shift; my $info = $weather->{results}; my @map = ([city => ":city", 'string'], [state => ":state", 'string'], [zip => ":zip", 'string'], [pic => ":picture", 'string'], [url => ":url", 'uri'], [cond => ":condition", 'string'], [temp => ":tempurature", 'integer'], [wind => ":wind", 'string'], [dewp => ":dewPoint", 'integer'], [humi => ":humidity", 'integer'], [visb => ":visiblity", 'string'], [baro => ":barometricPressure", 'string'], [heat => ":heatIndex", 'integer']); my $first = 1; print "["; for my $how ( @map ){ my ($key, $predicate, $type) = @{$how}; my $value = $info->{$key}; if ( $value ){ $value =~ s%%%; $value =~ s% % %; $value =~ s% +$%%; print ";\n " unless $first; $first = 0; if ( $type eq 'string' ){ $value = qq("$value"); } elsif ( $type eq 'integer' ){ $value = "$value"; } elsif ( $type eq 'uri' ){ $value = "<$value>"; } print "$predicate $value"; } } print "].\n"; } sub usage { print STDERR "ERROR: ", shift, "\n"; print STDERR < -s ] [-z ] Either , or must be provided. By example: $0 -c 'Boston' -s 'MA' $0 -z 02476 EOM return 1; }