# HG changeset patch # User Matti Hamalainen # Date 1486456511 -7200 # Node ID a17b7fdd03c09fc5d2204b183451b59675a07bbe # Parent e28c3347a9480dcd664d60c175890f97282e584f fetch_weather: Simplify and check for existence of output file before trying to read the old one. diff -r e28c3347a948 -r a17b7fdd03c0 fetch_weather.pl --- 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)';