Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How can I redirect STDOUT and STDERR from a program on WIN32?

( #4913=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Mar 06, 2000 at 00:22 UTC
Q&A  > programs and processes


Description:

I'm using backticks to call javac on Win32. I need to redirect all warnings and errors (i.e., STDERR output) from the spawned process into a perl string, so that I can print them out on the screen, write them to a file, etc.

Answer: How can I redirect STDOUT and STDERR from a program on WIN32?
contributed by tilly

I have used IPC::Open3 for this and found it worked well both on Unix and Windows. If $cmd has your command then:

use IPC::Open3; open3("<&STDIN", \*CAPTURE, \*CAPTURE, $cmd) or die "Cannot run $cmd: +$!";
Now you are reading both STDERR and STDOUT of the file from CAPTURE.

I don't know how to do this and get return codes as well though, the return of open3 is a process ID. Also note that you need to be careful with mixing filehandles. Should you try to get output while it is giving you input, programs take a while to get bored with that sort of silliness...

Answer: How can I redirect STDOUT and STDERR from a program on WIN32?
contributed by Anonymous Monk

It can be done by redirecting STDERR to STDOUT: my $output = `program 2>&1`; print $output;

Answer: How can I redirect STDOUT and STDERR from a program on WIN32?
contributed by dannoura

Simplest is from the command window. For STDOUT:

myscript.pl>myfile.txt

For STDERR

myscript.pl 2>myfile.txt
Answer: How can I redirect STDOUT and STDERR from a program on WIN32?
contributed by Anonymous Monk

I wrote this for unix:

sub phork {
  # we will call fork and return a pid.  The child will exec with all args 
  # and suppress the child's output (with /dev/null);
  my $pid;
  
  if ($pid = fork) {                    # fork the process;
    #parent
    return $pid;
  }else    
  {
    #child
    die "CANNOT FORK!!\n" unless defined $pid;
    open(STDOUT, "/dev/null");          # suppressing output  
    open(STDERR, "/dev/null");          # suppressing output
    {exec(@_);};                        # calls exec with current @_
    exit(1);                            # exec may maybe fail... maybe.      
  }  
}   
now, if you used the opens to open pipes, or to open files you want the output in, I'm thinking it should work in windows too.

I have a question, however, regarding how one would just disregard STDOUT and STDERR under windows using this function (there's no /dev/null of course).

Also, when you reap the forked process, you can then get the return value ($exit_value = $? >> 8;) if you don't want asynchronocity, then replace exec with system, and you can get the return value directly from that.

-Daniel

Answer: How can I redirect STDOUT and STDERR from a program on WIN32?
contributed by gfunk

It would probably be easier syntactically to get and use stderr.exe, which pipes both stderr and stdout, to stdout.

Answer: How can I redirect STDOUT and STDERR from a program on WIN32?
contributed by Anonymous Monk

In Windows 2000 you can capture both STDOUT and STDERR from an MS-DOS command by combining 1> and 2> like this:

C:\> MyCmd.cmd 1>2>MyLog.log
This keeps the log file text appear the same as it would have appeared on your screen. I only have Windows 2000, so I can't verify it on any other Microsoft OSes.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (7)
    As of 2015-07-03 11:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (51 votes), past polls