这个perl脚本背后的目的是首先重构一个.cpp
文件,然后编译整个包。如果一切顺利,则转到下一个文件,否则从backup
目录中替换原始文件,依此类推。以下是运行包的makefile
的perl脚本。
@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 $_";
}
}
该脚本一直运行,直到我收到一个包含编译器错误的“重构”文件。脚本无法捕获make
我猜测返回的错误。或者我错过了什么。
答案 0 :(得分:2)
几乎可以肯定的是,make错误会转到STDERR,后者不会被反引号捕获。使用Capture::Tiny可以轻松捕获两个输出流。
答案 1 :(得分:1)
如果使用system()
来调用make,则可以检查make是否成功。见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; }