in reply to How to cpture output of interactive command using perl.?

On Unix-like systems you could try something like this:
use File::Temp qw(tempfile); my ($fh, $filename) = tempfile(); close $fh; system("your_command | tee $filename"); # then open $filename for reading, it contains the output

But since now the STDOUT of your program is connected to a pipe, it might do a bit more buffering, which is bad for the user experience on the interactive program.

Replies are listed 'Best First'.
Re^2: How to cpture output of interactive command using perl.?
by xdg (Monsignor) on Dec 10, 2008 at 15:16 UTC
    On Unix-like systems...

    For a portable, pure-perl approach, there is the Tee module.

    use Tee; use File::Temp qw(tempfile); my ($fh, $filename) = tempfile(); close $fh; tee("your_command", $filename);

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re^2: How to cpture output of interactive command using perl.?
by pobocks (Chaplain) on Dec 10, 2008 at 14:14 UTC
    Could you not fix the buffering problem by turning on autoflush?
    ... $| = 1; #Autoflush on system("your_command | tee $filename"); $| = 0; #Autoflush off
    for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";