changeset 438:a17b7fdd03c0

fetch_weather: Simplify and check for existence of output file before trying to read the old one.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 07 Feb 2017 10:35:11 +0200
parents e28c3347a948
children ac66900fd066
files fetch_weather.pl
diffstat 1 files changed, 14 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/fetch_weather.pl	Mon Jan 09 09:23:05 2017 +0200
+++ b/fetch_weather.pl	Tue Feb 07 10:35:11 2017 +0200
@@ -301,18 +301,22 @@
 ###
 if (opt_chk_valid("outfile", 1))
 {
-  my $str = read_text(opt_get("outfile"));
-  if (defined($str))
+  my $filename = opt_get("outfile");
+  if (-e "$filename")
   {
-    foreach my $line (split(/\s*\n\s*/, $str))
+    my $str = read_text($filename);
+    if (defined($str))
     {
-      my @mtmp = split(/\|/, $line, -1);
-      if (scalar(\@mtmp) >= 3)
+      foreach my $line (split(/\s*\n\s*/, $str))
       {
-        $weatherdata->{shift @mtmp} = \@mtmp;
+        my @mtmp = split(/\|/, $line, -1);
+        if (scalar(\@mtmp) >= 3)
+        {
+          $weatherdata->{shift @mtmp} = \@mtmp;
+        }
       }
+      print STDERR scalar(keys %$weatherdata)." old records reloaded.\n" if (opt_get_int("debug") > 0);
     }
-    print STDERR scalar(keys %$weatherdata)." old records reloaded.\n" if (opt_get_int("debug") > 0);
   }
 }
 
@@ -601,9 +605,10 @@
 ###
 if (opt_chk_valid("outfile", 1))
 {
-  print STDERR "Dumping data to output file '".opt_get("outfile")."'\n" if (opt_get_int("debug") > 0);
+  my $filename = opt_get("outfile");
+  print STDERR "Dumping data to output file '".$filename."'\n" if (opt_get_int("debug") > 0);
   close(STDOUT);
-  open(STDOUT, '>', opt_get("outfile")) or die("Could not open output file '".opt_get("outfile")."': $!\n");
+  open(STDOUT, '>', $filename) or die("Could not open output file '".$filename."': $!\n");
 }
 
 binmode STDOUT, ':encoding(utf-8)';