How to capture Perl output on Windows (Strawberry perl)

No matter what I do, the output gets printed to a command window instead of being captured. I'm using Strawberry Perl on Windows and trying to capture the output of ffprobe to do some batch conversions.

In every instance the command works properly, I just can't capture it. Output goes to the command window and the return value seems to be an empty string.

$output = `ffprobe.exe "$file"`; # nope

$output = qx/ffprobe.exe "$file"/; # nope

$return = system("ffprobe.exe "$file" > output.txt") # nope, and $return is 0

If I open a command window and run it like this:

perl myscript.pl > output.txt

output.txt contains things the script itself prints (like if I do "print 'command output starts here:'; it will contain that), but none of the program output.

Since I do this on Linux all the time it must be a quirk of Windows, I just can't figure out what.


Update, solved

It turns out that output was being sent to STDERR. I was able to capture it with IPC::Run3. Nothing else worked, so I guess I'm calling IPC::Run3 my solution for this problem on Windows. I'll leave this post here in case anyone has the same problem.

use IPC::Run3;
my ($stdout, $stderr);
$run = run3($command, undef, $stdout, $stderr); # backslashes are important here
say "output was $stderr"; # works

您可以简单地将STDERR重定向到STDOUT

$output = `ffprobe.exe "$file" 2>&1`
链接地址: http://www.djcxy.com/p/47208.html

上一篇: 对64位内存位置的访问无效

下一篇: 如何在Windows上捕获Perl输出(Strawberry Perl)