perl文件处理程序不写入文件

时间:2015-06-11 20:17:38

标签: perl

下面的文件处理程序FILE不起作用,我错过了一些明显的东西吗?我无法写入$ failed_launches文件?

open(my $data, '<', $file) or die "Could not open '$file' $!\n";
while (my $line = <$data>) {
    chomp $line;

    if ($csv->parse($line)) {

      my @fields = $csv->fields();
       foreach ($fields[1] =~ /Problems/ || $fields[1] =~ /Failure/) {
          open (FILE, ">>", "$failed_launches") or die "can't open $failed_launches $!";
          while ($line = <FILE>){
          print "$line\n";
          }
          close (FILE);
          $status = 1;
          $msg = "CMR:'$fields[0]' unsuccessful, contact SE team";
          SendStatus($fields[2], $status, $msg);

       }
       foreach ($fields[1] =~ /Success/){
          $status = 0;
          $msg = "CMR:'$fields[0]' was successful!";
          SendStatus($fields[2], $status, $msg);
       }

    } else {
        warn "Line could not be parsed: $line\n";
      }

2 个答案:

答案 0 :(得分:2)

在这里打开FILE进行写作:

open (FILE, ">>", "$failed_launches") or die "can't open $failed_launches $!";

但是在这里你尝试阅读FILE;你从来没有写过任何东西!

while ($line = <FILE>){

答案 1 :(得分:0)

您正在打开FILE进行写作,但您没有写信给它。相反,你正在阅读它。如果您想通过FILE语句写入print。它以这种方式完成

print FILE "text";

我认为您想要做的是将$line写入FILE。你应该这样做

open (FILE, ">>", "$failed_launches") or die "can't open $failed_launches $!";
print FILE "$line\n";    
close (FILE);