This seems to work for me with Strawberry Perl 5.10 and cygwin. I output both process IDs to ensure I'm looking at the right things. I also put something in $exe so there's a command to execute. Curiously, open2 works even when $exe
is undef and still returns a PID that isn't the parent process ID.
use IPC::Open2;
use Symbol qw(gensym);
$exe = 'cmd.exe /c dir /b';
my $in = gensym();
my $out = gensym();
my $pid = open2($out, $in, $exe);
print "I am pid $$: open2 is pid $pid\n";
close $in;
print <$out>;
waitpid $pid, 0;
You don't need the gensym stuff. open2 will autogenerate the filehandles if its arguments are lvalues that are undef.