tags:

views:

247

answers:

2

I have the following script running in Perl 5.10 in cygwin:

use IPC::Open2;
use Symbol qw(gensym);

my $in = gensym();
my $out = gensym();
my $pid = open2($out, $in, "$exe");

waitpid $pid, 0;

The value of $pid is the the PID of the perl process running, not that of the executable pointed to by $exe. Any ideas?

+2  A: 

I just ran:

#!/usr/bin/perl

use strict;
use warnings;

use IPC::Open2;

my ($in, $out);

my $pid = open2($out, $in, ls => qw(-R /));

warn $pid, "\n";

waitpid $pid, 0;

__END__

and observed:

     2916    2620    2916       2912  con 1003 14:49:56 /usr/bin/perl
O    2088    2916    2916       4064  con 1003 14:49:57 /usr/bin/ls

Why are you using the gensym stuff anyway?

Sinan Ünür
+1  A: 

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.

brian d foy