Mercurial > hg > egg-tcls
comparison fetch_weather.pl @ 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 | 3c42ad35e157 |
children | 19fe8ef0a902 |
comparison
equal
deleted
inserted
replaced
437:e28c3347a948 | 438:a17b7fdd03c0 |
---|---|
299 ### | 299 ### |
300 ### Load already cached data | 300 ### Load already cached data |
301 ### | 301 ### |
302 if (opt_chk_valid("outfile", 1)) | 302 if (opt_chk_valid("outfile", 1)) |
303 { | 303 { |
304 my $str = read_text(opt_get("outfile")); | 304 my $filename = opt_get("outfile"); |
305 if (defined($str)) | 305 if (-e "$filename") |
306 { | 306 { |
307 foreach my $line (split(/\s*\n\s*/, $str)) | 307 my $str = read_text($filename); |
308 { | 308 if (defined($str)) |
309 my @mtmp = split(/\|/, $line, -1); | 309 { |
310 if (scalar(\@mtmp) >= 3) | 310 foreach my $line (split(/\s*\n\s*/, $str)) |
311 { | 311 { |
312 $weatherdata->{shift @mtmp} = \@mtmp; | 312 my @mtmp = split(/\|/, $line, -1); |
313 } | 313 if (scalar(\@mtmp) >= 3) |
314 } | 314 { |
315 print STDERR scalar(keys %$weatherdata)." old records reloaded.\n" if (opt_get_int("debug") > 0); | 315 $weatherdata->{shift @mtmp} = \@mtmp; |
316 } | |
317 } | |
318 print STDERR scalar(keys %$weatherdata)." old records reloaded.\n" if (opt_get_int("debug") > 0); | |
319 } | |
316 } | 320 } |
317 } | 321 } |
318 | 322 |
319 | 323 |
320 ### | 324 ### |
599 ### | 603 ### |
600 ### Output | 604 ### Output |
601 ### | 605 ### |
602 if (opt_chk_valid("outfile", 1)) | 606 if (opt_chk_valid("outfile", 1)) |
603 { | 607 { |
604 print STDERR "Dumping data to output file '".opt_get("outfile")."'\n" if (opt_get_int("debug") > 0); | 608 my $filename = opt_get("outfile"); |
609 print STDERR "Dumping data to output file '".$filename."'\n" if (opt_get_int("debug") > 0); | |
605 close(STDOUT); | 610 close(STDOUT); |
606 open(STDOUT, '>', opt_get("outfile")) or die("Could not open output file '".opt_get("outfile")."': $!\n"); | 611 open(STDOUT, '>', $filename) or die("Could not open output file '".$filename."': $!\n"); |
607 } | 612 } |
608 | 613 |
609 binmode STDOUT, ':encoding(utf-8)'; | 614 binmode STDOUT, ':encoding(utf-8)'; |
610 | 615 |
611 foreach my $key (sort { $a cmp $b } keys %$weatherdata) | 616 foreach my $key (sort { $a cmp $b } keys %$weatherdata) |