Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by Anonymous Monk
on Mar 06, 2000 at 00:22 UTC ( #4913=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (programs and processes)

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.

Originally posted as a Categorized Question.

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

Replies are listed 'Best First'.
Re: How can I redirect STDOUT and STDERR from a program on WIN32?
by tilly (Archbishop) on Aug 10, 2000 at 23:32 UTC
    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...

      any idea about getting the return code ? :)
Re: How can I redirect STDOUT and STDERR from a program on WIN32?
by Anonymous Monk on Mar 11, 2000 at 22:43 UTC
    It can be done by redirecting STDERR to STDOUT: my $output = `program 2>&1`; print $output;
Re: How can I redirect STDOUT and STDERR from a program on WIN32?
by dannoura (Pilgrim) on Apr 30, 2004 at 17:06 UTC

    Simplest is from the command window. For STDOUT:

    myscript.pl>myfile.txt

    For STDERR

    myscript.pl 2>myfile.txt
Re: How can I redirect STDOUT and STDERR from a program on WIN32?
by Anonymous Monk on Jan 24, 2001 at 07:26 UTC
    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

      On windows the null device is simply 'NUL' I belive.

      To make it portable do this instead.

      use File::Spec; sub phork { #... open(STDOUT, '> ' . File::Spec->devnull); open(STDERR, '> ' . File::Spec->devnull); #... }
      That _should_ do it.
Re: How can I redirect STDOUT and STDERR from a program on WIN32?
by gfunk (Initiate) on Jan 26, 2009 at 19:35 UTC
    It would probably be easier syntactically to get and use stderr.exe, which pipes both stderr and stdout, to stdout.
Re: How can I redirect STDOUT and STDERR from a program on WIN32?
by Anonymous Monk on Jul 22, 2004 at 19:24 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://4913]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2021-04-18 21:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?