【发布时间】:2015-11-16 12:48:53
【问题描述】:
无论我做什么,输出都会打印到命令窗口而不是被捕获。我在 Windows 上使用 Strawberry Perl 并尝试捕获 ffprobe 的输出以进行一些批量转换。
在每种情况下,该命令都能正常工作,但我无法捕获它。输出到命令窗口,返回值似乎是一个空字符串。
$output = `ffprobe.exe "$file"`; # nope
$output = qx/ffprobe.exe "$file"/; # nope
$return = system("ffprobe.exe \"$file\" > output.txt") # nope, and $return is 0
如果我打开一个命令窗口并像这样运行它:
perl myscript.pl > output.txt
output.txt 包含脚本本身打印的内容(例如,如果我执行"print 'command output starts here:';,它将包含该内容),但没有任何程序输出。
由于我一直在 Linux 上这样做,这一定是 Windows 的一个怪癖,我就是不知道是什么。
更新,已解决
事实证明,输出被发送到 STDERR。我能够用 IPC::Run3。没有其他工作,所以我想我正在调用 IPC::Run3 我在 Windows 上解决此问题的解决方案。我会把这篇文章留在这里,以防有人遇到同样的问题。
use IPC::Run3;
my ($stdout, $stderr);
$run = run3($command, undef, \$stdout, \$stderr); # backslashes are important here
say "output was $stderr"; # works
【问题讨论】:
-
会不会是将其输出发送到 STDERR 而不是 STDOUT?
-
@RonBergin 就是这样,这是捕获输出的又一次冒险,但我终于用 IPC::Run3 做到了。
-
你不妨自己回答吧:)
-
将 STDERR 重定向到 STDOUT:
ffprobe.exe file 1> output.txt 2>&1 -
@SinanÜnür 不幸的是,在 Windows 上并不那么容易;那是行不通的。
标签: windows perl strawberry-perl