开发者

problem while capturing the error of `make`

开发者 https://www.devze.com 2023-03-25 23:38 出处:网络
The purpose behind this perl script is to first refactor a .cpp file and then compile the whole package. If all goes well then move on to the next file otherwise replace the original file from the bac

The purpose behind this perl script is to first refactor a .cpp file and then compile the whole package. If all goes well then move on to the next file otherwise replace the original file from the backup directory, and so on. Following is the perl script for running the makefile of a pa开发者_Go百科ckage.

@lcpp = `ls *.cpp`; 
chomp(@lcpp);
foreach (@lcpp) {
print "processing file $_ ...";
`cp demac_dir/$_ .`;
if(2 == `make`) {
  print "\n\t\t\tError in the file\n";
  `cp backup/$_ .`;
  print "reverting back to the original file and building the package again";
  `make`;
}
else {#when successfully compiled
  print "successfully compiled the package with file $_";
}
}

The script runs until i get a 'refactored' file with compiler errors. The script is unable to capture the error returned by make i guess. Or am i missing something.


Almost for sure make errors go to STDERR, which is not captured by backticks. Use Capture::Tiny for easy capture of both output streams.


If you use system() to invoke make, you can check whether make succeeded. see perldoc -f system:

    @args = ("command", "arg1", "arg2");
    system(@args) == 0
         or die "system @args failed: $?"

You can check all the failure possibilities by inspecting $?
like this:

    if ($? == -1) {
        print "failed to execute: $!\n";
    }
    elsif ($? & 127) {
        printf "child died with signal %d, %s coredump\n",
            ($? & 127),  ($? & 128) ? 'with' : 'without';
    }
    else {
        printf "child exited with value %d\n", $? >> 8;
    }

0

精彩评论

暂无评论...
验证码 换一张
取 消