Perl 捕获“make”错误时出现问题`

Perl 捕获“make”错误时出现问题`,perl,makefile,Perl,Makefile,这个perl脚本的目的是首先重构.cpp文件,然后编译整个包。如果一切顺利,则转到下一个文件,否则从备份目录替换原始文件,依此类推。下面是用于运行包的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\

这个perl脚本的目的是首先重构
.cpp
文件,然后编译整个包。如果一切顺利,则转到下一个文件,否则从
备份
目录替换原始文件,依此类推。下面是用于运行包的
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
返回的错误。或者我遗漏了什么。

几乎可以肯定的是,将错误转到STDERR,而STDERR不会被反勾号捕获。用于轻松捕获两个输出流。

几乎可以肯定的是,将错误转到STDERR,而STDERR不会被反勾号捕获。用于轻松捕获两个输出流。

如果使用
system()
调用make,则可以检查make是否成功。请参见perldoc-f系统:

@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; } @args=(“命令”、“arg1”、“arg2”); 系统(@args)==0 或“系统@args失败:$?” 您可以通过检查$? 这样地: 如果($?==-1){ 打印“执行失败:$!\n”; } elsif($?&127){ printf“孩子死了,信号为%d,%s coredump\n”, ($?&127),($?&128)?“有”:“无”; } 否则{ printf“值为%d\n的子项已退出,$?>>8; } 如果使用
system()
调用make,可以检查make是否成功。请参见perldoc-f系统:

@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; } @args=(“命令”、“arg1”、“arg2”); 系统(@args)==0 或“系统@args失败:$?” 您可以通过检查$? 这样地: 如果($?==-1){ 打印“执行失败:$!\n”; } elsif($?&127){ printf“孩子死了,信号为%d,%s coredump\n”, ($?&127),($?&128)?“有”:“无”; } 否则{ printf“值为%d\n的子项已退出,$?>>8; }
我只是想,如果make不成功,那么脚本应该“知道”并执行必要的操作。是的,你是对的,错误消息转到'stderr'@Aditya Kumar-我想你应该解析输出,看看发生了什么错误。如果您需要的只是成功,那么检查
$?
可能是最快的方法。我只希望如果make不成功,那么脚本应该“知道”并执行所需的操作。是的,你是对的,错误消息转到'stderr'@Aditya Kumar-我想你应该解析输出,看看发生了什么错误。如果你只需要成功,检查
$?
可能是最快的方法。为什么要回退而不只是打电话呢?好的。。。我现在正在尝试system(),虽然有这么多的系统调用,但使用perl似乎有些过头了。将第一行与
my@lcpp=glob“*.cpp”交换,那么您就可以得到一个非常平滑的数组,而无需咀嚼它。用于复制/移动命令。我想也有一些好东西可以做。为什么不打电话就回嘴呢?好的。。。我现在正在尝试system(),虽然有这么多的系统调用,但使用perl似乎有些过头了。将第一行与
my@lcpp=glob“*.cpp”交换,那么您就可以得到一个非常平滑的数组,而无需咀嚼它。用于复制/移动命令。我想也有适合制作的东西。现在我正在尝试system()。。。让我们看看进展如何。。。谢谢你的建议是的system()函数运行得很好。。。我应该在3-4小时前问你。。。谢谢。现在我正在尝试system()。。。让我们看看进展如何。。。谢谢你的建议是的system()函数运行得很好。。。我应该在3-4小时前问你。。。谢谢