http://qs1969.pair.com?node_id=76335

this_hamster has asked for the wisdom of the Perl Monks concerning the following question:

In the below perl script, I fork, then have the child start "cat" via open3(). The child does some I/O with the "cat" process, then exits. I'm using Cygwin's cat.exe .
my $CMD = 'cat'; use IO::Handle; use IPC::Open3 qw( open3 ); use strict; my $pid = fork(); if ( !defined($pid) ) { die( "$$: Fork failed: $!\n" ); } # parent if ( $pid ) { print( "$$ FORKED $pid\n" ); } # child else { my( $pw, $pr, $pe, $pipe ); foreach $pipe ( $pw, $pr, $pe ) { $pipe = new IO::Handle; } my $pipe_pid = open3( $pw, $pr, $pe, $CMD ) or die( "$$: failed to handle to $CMD: $!\n" ); print( "$$:\tpipe pid = $pipe_pid\n"); $pw->print( "hostinfo\n" ); $pw->print( "quit\n" ); print( "$$: out: ", $pr->getline(), "\n" ); exit(0); } waitpid( $pid, 0 ); print( "$$ caught $pid: $?\n" ); print( "PARENT $$ exiting\n" );
This works fine on Unix (perl 5.6.0):
  $ ./t_iohand2
  8629 FORKED 8630
  8630:   pipe pid = 8631
  8630: out: hostinfo

  8629 caught 8630: 0
  PARENT 8629 exiting
However, it does this on Win32 (ActiveState Perl 5.6.0-623 and 5.6.1-625):
    D:>perl -w t_iohand2
    304 FORKED -1392
    am I in cat?
    #hangs here, so I type in "am I in cat?" to see if this is cat's STDIN/STDOUT
    am I in cat? #cat repeated the line back
    (hit ^D here to end cat)
    Use of uninitialized value in print at t_iohand2 line 35.
    -1392: pipe pid = 1400
    -1392: out:
    304 caught -1392: 0
    PARENT 304 exiting

The process tree for the Win32 output looks like this: 304 -(fork)-> -1392 -(open3)-> 1400

It looks like the STDIN/STDOUT of process 1400 (the open3() child) is getting mixed up with the STDIN/STDOUT of it's parent, -1392.

I'm using "cat" as an example here. In the actual script, $CMD is the path to a shell-like program (similar to csh) running on Win32.

This works fine on Cygwin perl 5.6.1-1, but my project wants to use ActiveState for a variety of reasons.

Thanks for any input!