in reply to Re: 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, it works.

and how can I get the output continuously?
it gives me the output after the command has been executed?

like this:
open (OUTPUT, "command |"); while (<OUTPUT>) #do something with output }
can I use $? here too?
  • Comment on Re^2: how can I redirect the output of a program and get the exit code too?
  • Download Code

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

    Yes you can, but only if you call close OUTPUT before. The close function will put the return value to $?.

      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.

        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.