将STDERR重定向到文件锁定该文件

时间:2015-11-12 21:20:30

标签: perl io

PERL Wizards,我正在尝试将STDERR重定向到我已打开的错误文件,如果我的程序行为不当。我遇到的问题是下面包含“#Help Here”的行以某种方式阻止我的程序删除空错误文件。几乎像这样绑定它们并防止删除/取消链接。您是否看到我可以调整我的代码以确保我可以删除0大小的错误文件? 谢谢!

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 个答案:

答案 0 :(得分:1)

我明白了。我在下面制作了STDERR:

当地* STDERR; 打开(STDERR,'&gt;',$ errFile)或死!$ ;;

它现在是例程的本地,并释放输出文件,以便稍后取消链接。