如何在Perl中读取外部命令的错误输出? [英] How can I read the error output of external commands in Perl?

查看:77
本文介绍了如何在Perl中读取外部命令的错误输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为较大的Perl程序的一部分,我正在对照参考文件检查文件夹中输入文件的 diff 命令的输出,其中空白输出(匹配项)是传递的结果,而diff的任何输出都是失败的结果。

As part of a larger Perl program, I am checking the outputs of diff commands of input files in a folder against reference files, where a blank output (a match) is a passing result, and any output from diff is a fail result.

问题是,如果目标文件夹的文件数不足,则diff例外

The issue is, if the target folder is short on the number of expected files, the exception diff throws doesn't come as output, creating false passes.

输出示例:

diff: /testfolder/Test-02/test-output.2: No such file or directory

Test-01:通过

Test-01: PASS

Test-02:通过

Test-02: PASS

代码如下:

$command = "(diff call on 2 files)";
my @output = `$command`;
print "Test-02: ";
$toPrint = "PASS";
foreach my $x (@output) {
    if ($x =~ /./) {
        $toPrint = "FAIL";
    }
}

如果存在以下情况,这是一项失败的快速黑客工作 diff 调用的任何输出。有没有办法检查反引号中调用的命令引发的异常?

This is a quick hackery job to fail if there is any output from the diff call. Is there a way to check for exceptions thrown by the command called in the backticks?

推荐答案

程序本身不能抛出 exceptions,但它们可以返回非零错误代码。您可以使用$?在Perl中检查带有反引号或 system()运行的程序的错误代码:

Programs themselves can't throw "exceptions", but they can return nonzero error codes. You can check the error code of a program run with backticks or system() in Perl using $?:

$toPrint = "FAIL" if $?;

(在测试 @output 。)

这篇关于如何在Perl中读取外部命令的错误输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆