Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to print the output from a systme call while it is running rather than waiting for the system call to complete?
  • Comment on print output from system command in real time

Replies are listed 'Best First'.
Re: print output from system command in real time
by moot (Chaplain) on May 10, 2005 at 18:34 UTC
    You could try something like
    open SYS, "command |" or die "$!"; while (<SYS>) { print } close SYS;
    but a lot depends on whether 'command' buffers its output, among other variables. You should probably read perldoc perlipc for more insight.
      Thanks but that did not work. I built a standalone exe from the following code. Oh and I am doing this on windowz to make it even more fun.
      until($cnt == "100"){ print "COUNT IS $cnt\n"; sleep 5; $cnt++; }
      I then call the exe within perl. With the code you supplied it still waits until completion before printing. I even tried setting autoflush with the code below.
      open SYS, "c:\\temp\\junk.exe |" or die "$!"; $oldfh=select(SYS); $| = 1; select($oldfh); $| = 1; while (<SYS>) { print } close SYS;
Re: print output from system command in real time
by Roy Johnson (Monsignor) on May 10, 2005 at 19:06 UTC
    If you just call system, it should run and print normally. Do you need to capture the output as well as have it print?

    Caution: Contents may have been coded under pressure.
      Thanks for the suggestion, but no go. system did the same thing. It just waited for the exe to finish before printing any output. Unfortunatly I don't have access to the code of the actual exe I will be running so no way to modify that code.