7

I have a program that is supposed to run a set of other programs and collect their output for logging purposes. Everything is working fine as long as there is output to standard out.

That leads me to my two problems:

  1. How can I capture both STDIN and STDERR of another program in one file?

  2. If there is no output at all (or output to STDERR only) the program will get stuck on the line:

    while (<$input>)
    

How can I make the program wait for possible output from another program of indeterminate run time, and still continue if there is no output by the time the program finishes execution.

Here is that section of the code

my $pid = open (my $input, '-|', "$prog $args")
        or push @errors, "A failute has occurred in $prog $args";
if(not @errors){
    while (<$input>){ #POSSIBLE LOCATION FOR HANG UP IF NO PRINTING IS DONE
        if($input =~ /^END\n$/){
            last;
        }
        print $fh $_;
    }
}
else{
    print $fh "An error has occurred with executing \"$prog $args\"";
}

note: $fh is my file handler used to write to my log file, and @errors is used for internally reporting errors in my program.

EDIT: One problem I experienced with trying the Proc::Reliable route is that it seems to have after effects on STDOUT and STDERR, and I would need to fix those after each call to run. This leads to another problem in my code. I am running in parallel and any change to STDOUT and STDERR effect all of the threads (using Windows at least).

IO::CaptureOutput has the same issue for me, but it tries to correct the STDOUT and STDERR jumbling internally and causes the following error to occur:

Couldn't remove temp file 'C:\Users\SBLAKE~1\AppData\Local\Temp\m8E3pUGfOS' - Permission denied at C:/Perl/site/lib/IO/CaptureOutput.pm line 82
Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129
Shawn Blakesley
  • 1,743
  • 1
  • 17
  • 33
  • possible duplicate of [How do you capture stderr, stdout, and the exit code all at once, in Perl?](http://stackoverflow.com/questions/109124/how-do-you-capture-stderr-stdout-and-the-exit-code-all-at-once-in-perl) – daxim Aug 08 '12 at 18:45

2 Answers2

1

Look at Proc::Reliable, it seems to be what you want.

See also:

Community
  • 1
  • 1
h4ck3rm1k3
  • 2,060
  • 22
  • 33
1

If you launch from a POSIX-like shell, then the 2>&1 notation sends standard error to the same place as the standard output:

some-program-to-be-logged 2>&1 | perl logger.pl -o /chosen/log.file

Both standard output and standard error are sent to the standard input of the Perl logging script.

The input loop in the logger script will hang waiting until there is input to read, or until all the processes with the pipe open for writing close the pipe (typically because they exit).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278