in reply to Re^3: how can I redirect the output of a program and get the exit code too?
in thread how can I redirect the output of a program and get the exit code too?

thanks again,

everithing works, but i'm writing the output to a textwindow (Tk: Scrolled text) and it refreshes it only after the redirected program ends. It *, is it possible to solve this problem?

Edited by Chady -- converted pre to p tags.

  • Comment on Re^4: how can I redirect the output of a program and get the exit code too?

Replies are listed 'Best First'.
Re^5: how can I redirect the output of a program and get the exit code too?
by ambrus (Abbot) on Jun 23, 2004 at 09:09 UTC

    That probably depends on the external program you are calling.

    Libc normally sets up a stream buffered, more precisely line buffered if it's connected to a terminal, full buffered otherwise. This means that unless the program you call will do explicitly otherwise, you'll get the output only after each newline if it's connected to a terminal, but the newline does not flush the buffer if it's connected to a pipe as in your case, so you'll get the output chunk by chunk. Some programs (like cat) are intelligent enought to smartly override this, but some programs just leave it as is (the most annoying one being gnu tr).

    If the program you call is a perl script you have written, you can edit it to flush the output whenever you need it to appear (with use IO "Hanlder"; flush STDOUT; on newer perls). If it's a C program you wrote, you can flush the output with fflush(stdout), or force line buffering with setlinebuf(stdout); (I'm not sure in this one). If it's a program you cannot change, you probably can't do many things, except maybe to call if through a virtual terminal, which I don't know how to do from perl, and can have other effects.

      you dont understand me. everything works, I have the output
       of the program (the program is in perl, my is in perl/tk, 
      it is a GUI). I can write to the textwindow, but my program 
      does nothing (like frozen) till the other program ends. and 
      this is a problem, while the other program runs,my should 
      too ! I tried to run the program in the background (&), but 
      nothing.
      

        As far as I can tell, that might be an issue with the windowing system you use, it might not refresh the text control until it returns to the main control loop. I don't know much about gui programming, so I can't tell.

        Also, it can be a problem wit buffering or that you are reading the output line-by-line with the diamond operator.

        You shoud probably post it as a new SOPW, as this is a different question than the original one.