在 perl 脚本中,我必须调用计算 psnr 的简单程序。我这样做:
$psnr_command_line = '';
$psnr_command_line = "/snr file1 file2 width height"
创建命令行后,我只需在 perl 中调用系统:
$psnr_Y = system ($psnr_command_line);
在这种情况下,如果命令成功执行,系统将返回 0。但我需要在 $psnr_Y 中存储其他内容。当然,我需要程序内部计算出的 psnr 的实际值。
所以,到目前为止,我有以下内容。在简单的 snr 程序中,我打印了计算结果(在本例中为 psnr),因此在我运行 perl 脚本后,在命令行上打印了如下内容: P=65535.000000 MSE=304531.729854 PSNR=41.493141dB
我在徘徊,是否可以通过某种方式将 PSNR 值转换为 perl 中的 $psnr_Y 值?也许以某种方式将打印重定向到 perl 的变量,而不是简单地解析该行以获得 PSNR?
有什么想法吗?欢迎任何其他方法。
谢谢。
最佳答案
我认为您正在寻找反引号运算符:
$psnr_Y = `$psnr_command_line`;
与返回命令的系统退出代码的函数system()
不同,反引号运算符返回程序的输出(即它发送到STDOUT
的内容) .
命令执行后,您可以使用正则表达式解析结果,例如:
my ($val) = ($psnr_Y =~ m/PSNR=(\S+)/);
引用:perlop
- Quote-Like-Operators :
STRING
A string which is (possibly) interpolated and then executed as a system command with/bin/sh
or its equivalent. Shell wildcards, pipes, and redirections will be honored. The collected standard output of the command is returned; standard error is unaffected.
关于perl - 如何获取在简单程序中计算的变量以在 perl 脚本中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58572804/