【发布时间】:2011-08-04 08:42:18
【问题描述】:
这个 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 返回的错误。还是我错过了什么。
【问题讨论】:
-
为什么要反引号而不是直接调用
system? -
好的……我正在尝试 system()
-
虽然有这么多的系统调用,但似乎使用 perl 是多余的。将第一行与
my @lcpp = glob "*.cpp";交换,然后您将获得一个非常平滑的数组,而无需对其进行咀嚼。 File::Copy 用于复制/移动命令。我怀疑 make 也有一些不错的东西。