problem while capturing the error of `make`
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; }
精彩评论