例如,我正在尝试执行系统命令
system('git clone .....' );
if ($?) {
croak('Error while cloning git repository');
}
这里我检查结果是否成功,但是如何不从系统命令输出错误,例如在我的例子中,我可以得到类似的东西
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
来自执行的命令。
我需要将此错误放入变量中并抑制它(不要将其打印到终端)
然后检查此错误消息。
或者至少压制它。
我需要通过以下方式测试这样的子程序
dies_ok { MyModule::sub_uses_system_command() } 'Died :(';
有可能得到这样的结果吗?
提前谢谢。
system
只返回已执行程序的退出状态,如果想得到标准输出可以使用 qx/command/
或反引号来执行命令:
my $result = `git clone [...] 2>&1`
您应该注意,执行命令的
qx/command/
和反引号形式仅返回STDOUT,因此如果您想捕获STDERR,您需要在命令中将STDERR重定向到STDOUT。
而不是
system
,请使用 qx
来捕获命令的输出。看起来您还想捕获 stderr,因此使用标准 2>&1
将 stderr 复制到 stdout。
$var = qx( git clone ... 2>&1 )
如果您需要执行多个输出到 STDERR/STDOUT 的测试,您可以在一个块内重定向它们并在其中运行所有这些测试。这是一个基本示例。
sub use_system {
system("asdfasdf asdfasdf");
croak('this error') if $?;
}
{
open my $stderr, '>', 'temp.fil' or die $!;
local *STDERR = $stderr;
dies_ok { use_system() } 'Died :(';
# or even
eval { use_system(); };
like ($@, qr/this error/, "function failed with 'this error'");
}
warn "STDERR back to normal\n";
您可以重定向输出:
system(`git clone https://example.com/some/path >/dev/null 2>&1`);
如果命令是动态构建的(需要参数),则
system()
可以说比 qx//
更好,因为您不需要参与插值和引用。您可以将参数传递给 shell 并在 -c
的参数中将它们用作 shell 变量:
system('sh', '-c', 'echo "$1" "$2" >/dev/null 2>&1', '-', $some, $args)