0

In my Perl code, I use a system command to run a script. I'm using Gtk2::Perl and Glade to build a UI. I need the output of the command to be captured not just to the console (which Capture::Tiny does), but also to a TextView in my GUI.

system("command");

$stdout = tee{                         #This captures the output to the console
system("command");  
};

$textbuffer->set_text($stdout);       #This does set the TextView with the captured output, but *after* the capture is over. 

Any help would be greatly appreciated.

2
  • So what's the problem you're having? Commented Jun 30, 2015 at 9:06
  • I need the output of system to be captured in the TextView simultaneously with its capture on the console. That's not happening. Commented Jun 30, 2015 at 9:11

2 Answers 2

4

If you're trying to 'capture' the output of a system call, then I would suggest the best approach is to use open and open a filehandle to your process:

my $pid = open ( my $process_output, '-|', "command" ); 

Then you can read $process_output exactly as you would a file handle (bear in mind it'll block if there's no IO pending).

while ( <$process_output> ) { 
   print; 
}

close ( $process_output ); 

You can 'fake' the behaviour of system via the waitpid system call:

 waitpid ( $pid, 0 ); 

This will 'block' your main program until the system call has completed.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I suppose the use of open is my best bet. Will do this!
This is very useful but does not really solve my problem, actually - the output is not dynamically updated in the TextView as it is generated. There is a distinct delay, sadly.
Look up autoflush in the IO::handle docs. Open like this is already running in a parallel process.
2

What you want to do is not possible with system(). System() forks a new process and waits for it to terminate. Then your program continues (see manual). You could start a sub-process (executing whatever system() did for you) and read this sub-process' stdout. You could for example get inspired here: redirecting stdin/stdout from exec'ed process to pipe in Perl

1 Comment

Ah, I see. I expected to be able to do something with system(), as this capture is the only thing lacking. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.