2015-11-12 55 views
0

PERL嚮導,我試圖將STDERR重定向到一個錯誤文件,如果我的程序行爲異常,我已打開該文件。我遇到的問題是,下面包含「#Help Here」的行以某種方式阻止我的程序刪除空的錯誤文件。幾乎像這樣將它們聯繫起來並防止刪除/取消鏈接。你是否看到無論如何我可以調整我的代碼,以確保我可以刪除0大小的錯誤文件? 謝謝!將STDERR重定向到文件鎖定文件

foreach $inputFile (@inputFiles) { 

    #Open input and error files for reading and writing data 
    open (INFILE, '<', $inputFile) or die $!; 
    $errFile = $inputFile . "-error"; 
    open (ERRFILE, '>', $errFile) or die $!; 
    #Redirect STDERR from the console to the error log 
    close (STDERR); #Help here 
    open (STDERR, '>', $errFile) or die $!; #Help here 

    #Override default line delimiter of /n with custom delimiter 
    local $/ = '~~lineDelimiter~~'; 
     while ($inFileLine = <INFILE>) { 

     #Create an array of fieldnames from the line being processed  
     @fieldNames = $inFileLine =~ m(<\/(.*?)>)g; 
     #Create an array of data values from the line being processed 
     @fieldValues = $inFileLine =~ m(>([^<]+)<)g; 

     #Populate a variable with the dbid for the line being processed 
     @filter = @fieldValues[0]; 
     $queryDef = $session->BuildQuery("SWCR"); 
     $queryDef->BuildField("dbid"); 
     $queryFilter = $queryDef->BuildFilterOperator($CQPerlExt::CQ_BOOL_OP_AND); 
     $queryFilter->BuildFilter("RecordID",$CQPerlExt::CQ_COMP_OP_EQ, \@filter); 
     $queryResults = $session->BuildResultSet($queryDef); 
     $queryResults->Execute(); 
     while ($queryResults->MoveNext() == $CQPerlExt::CQ_SUCCESS) { 
      $dbid = $queryResults->GetColumnValue(1); 
              } 

     #Insert data for all fields after the SWCR (elements>=1)  
     $entity = $session->GetEntityByDbId("SWCR",$dbid); 
     $entity->EditEntity("AdminModify"); 
     $entity->SetFieldValue($fieldNames[$_],$fieldValues[$_]) for (1 .. $#fieldNames); 
     $entity->SetFieldValue("AdminModifyReason","Data from file: $inputFile was imported into this record per the MSTU/LMITS sync process."); 
     #Evaluate for validation errors 
     $trappedErrorValidate = $entity->Validate(); 
      if ($trappedErrorValidate ne "") { 
       print ERRFILE "The following record has not been imported due to the error code specified below:\n\n"; 
       #Remove the leading newline character of the record so it prints more clearly 
       $inFileLine =~ s/^\n//; 
       print ERRFILE "$inFileLine\n"; 
       print ERRFILE "\nError Code:$trappedErrorValidate\n"; 
       print ERRFILE "*********************************************************************************\n"; 
        $entity->Revert(); 
            } else { 
              $trappedErrorCommit =$entity->Commit(); 
            if ($trappedErrorCommit ne "") { 
             print ERRFILE "The following record has not been imported due to the error code specified below:\n\n"; 
             #Remove the leading newline character of the record so it prints more clearly 
             $inFileLine =~ s/^\n//; 
             print ERRFILE "$inFileLine\n"; 
             print ERRFILE "\nError Code:$trappedErrorCommit\n"; 
             print ERRFILE "*********************************************************************************\n"; 
                          } else { 
                  $recordCount++; 
                               } 
             } 
           }  


    close (INFILE); 
    close (ERRFILE); 

    #Add amount of records imported to the import log 
    open (IMPLOG, ">>", 'Import_Log.txt') or die $!; 
    print IMPLOG "$scriptStartTime: $recordCount record(s) imported into LMITS from $inputFile.\n"; 
    #Reset record count for each input file to use the counter 
    $recordCount = 0; 
    close (IMPLOG); 

            } 

# ############################################## 
# ##### Process administrative items and 
# ##### Close ClearQuest session 
# ############################################## 

#Mark each input file processed 
foreach $inputFile (@inputFiles) { 
    rename ($inputFile, $inputFile . "-processed"); 
       } 

#Remove blank error files 
opendir(DIR, 'c:\LMITS'); 
@errFiles = grep /error/, readdir DIR; 
closedir DIR; 

    foreach $errFile (@errFiles) { 
     $errFileSize = -s $errFile; 
     if ($errFileSize == 0) { #Help Here 
     unlink $errFile;    #Help Here 
         } 
        } 

回答

1

我明白了。我根據以下地方製作STDERR:

local * STDERR; open(STDERR,'>',$ errFile)或die $!;

它現在是本地的例程,並釋放輸出文件,所以我可以稍後解除鏈接。

相關問題