Sometimes there is a need to capture the output from another perl program and display it in your program. Especially when you use Pterl/Tk and want to invoke a perl program to do something, you want to capture the standard output of the other program “in real time.” Here is a way to do it:
program 1 (call it a):
#!/usr/local/bin/perl -w
$|=1; #flush the standard output
use strict;
sub printMsg {
foreach (0.. 10) {
sleep(1);
print "$_\n";
}
}
printMsg();
#### end of program
program 2
#!/usr/local/bin/perl -w
open(FD,"a|");
while (<FD>) {
print $_;
}
Idea is to use a pipe. Any comments?