in reply to Re: Re: Re: Can't spawn "cmd.exe": No error at
in thread Can't spawn "cmd.exe": No error at

To clarify, I am looking for the best way to capture all output from an arbitrary command run on windows via Perl with the maximum amount of error handling. My understanding is that system coupled with output redirection is the best way to do this, but I'd happily stand corrected if there's a better way. Otherwise, is:
system("foo > stdoutfile 2> stdoutfile");
the best way to capture all output from the command foo? Are there any buffer issues by having to specify the file twice rather than the Unix technique of copying the descriptor with 2>&1?

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Can't spawn "cmd.exe": No error at
by BrowserUk (Patriarch) on Jul 18, 2003 at 00:16 UTC

    There is nothing wrong with your syntax for redirecting STDOUT & STDERR to the same file.

    P:\test>perl -e"print 'This goes to stdout', $/; print STDERR 'This go +es to STDERR', $/;" 1>junk.log 2>&1 P:\test>type junk.log This goes to STDERR This goes to stdout

    One caveat is that in common with several other POSIX systems, you need to do this in the right order; that shown above. The other way around does not do the same thing.

    The other caveat, also common to many systems, is that depending upon the buffering stratecies used internally by the program you are running, you may find that the output from the two streams is not correctly interleaved. Often this shows up as STDERR output showing up before STDOUT lines, with the latter tending to come bunched together in blocks. This is because STDOUT os often buffered whereas STDERR is not. You'll notice in the above example the STDERR line precedes the STDOUT line.

    If the programs/commands in question are other perl scripts then you can correct this by disabling buffering on STDOUT, but if they are programs written in C (or other languages) to which you do not have the source, there is little or nothing you can do about this.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

Re: Re: Re: Re: Re: Can't spawn "cmd.exe": No error at
by mgibian (Acolyte) on Jul 18, 2003 at 00:13 UTC
    So now I am totally confused. I changed my code to use 1> stdoutfile 2> stdoutfile per the prior comment, and now I'm getting file being used by another process. If I use separate files for 1> and 2> it works. AND 2>&1 sure does seem to work?