Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

execute, show output as it's running, capture output for file as well

Status
Not open for further replies.

dougld

Programmer
Sep 13, 2002
2
0
0
GB
This problem is trickier than it sounds, at least to me, so please, no "@outPut=`myScript.pl 2&>1`;" type suggestions :)

All I want is to run my program, say counting.pl, from within another perl script so that the user sees the output:
1
2
3
.
.
.
as it occurs. I also want to save the output to write a file for later viewing.

I know! Simple! But although backticks return output, I can't force them to also write to STDOUT as the program runs. Various versions of "open" refuse to make a peep until it's all over. "System" puts everything to the screen but I can't figure out a way to capture it.

There are lots of suggestions in the books and online but most of them seem to ignore the unholy Win32 universe, which is what our clients are using. They promise what I want but none of the examples work. I'm about to give up and just them a choice of screen output or report generation but it seems like a weaselly thing to have to do.

Any suggestions?
 
Hi there!
Had the same problem you're having now, at least I think so!
I posted it into the forum and someone answered to me.
Here's what I got.

open(CMDOUTPUT, "someDosCommand |") or die "Unable to run command: someDosCommand |";
#You're opening a Filehandle that works somehow like a buffer
open(LOG, ">>$log") or die "Unable to open log file: $log";
#open logfile for writing
while (defined($line = <CMDOUTPUT>))
{
print $line;
print LOG $line;
}# end while (defined($line = <CMDOUTPUT>))

close(CMDOUTPUT);
close(LOG);
Hope I wrote it in right else look in Thread
Thread219-345951
Hope this helps,
busche

 
Parcival21 has it right. His method is setting up a pipe from the process to a file handle. A pipe is a way to send data to or from a process.

Here's a windows example of sending data to a process:

open(PIPE, &quot;| date&quot;) or die &quot;Can't open pipe: $!\n&quot;;
print PIPE &quot;09-13-02\n&quot;;
close(PIPE);
 
Hi raider,
your knowledge might be very helpful to me if you can explain me one thing.
The process I called in my script was created by a system call, but the code that was given to me posted the system call into double quotes without using anything to define it as a syscall. But it worked just fine.
Why do I not have to define my DosCommand as a syscall??

Thanks,
busche
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top