open2 under windows 
Author Message
 open2 under windows

Hello and tia:

  I am trying to get a bidirectional pipe working under both Unix and
Activestate 5.6.1 build 635. I thought my best bet would be
IPC::Open2, but so far it just hangs under Win2k. Here's the code.

# ipctest.pl
use strict;
use IPC::Open2;

my $pid;
my ($r, $w);
$pid = open2($r, $w, 'perl echo.pl');

print $w "Hello World\n";
close($w);

print STDOUT <$r>;

# echo.pl
use strict;
print while (<>) ;

  Thanks for the help.

  -Alan



Mon, 21 Nov 2005 21:22:11 GMT  
 open2 under windows

(snipped)

Quote:
> I am trying to get a bidirectional pipe working under both Unix and
> Activestate 5.6.1 build 635. I thought my best bet would be
> IPC::Open2, but so far it just hangs under Win2k. Here's the code.
> # ipctest.pl
> use strict;
> use IPC::Open2;
> my $pid;
> my ($r, $w);
> $pid = open2($r, $w, 'perl echo.pl');
> print $w "Hello World\n";
> close($w);
> print STDOUT <$r>;
> # echo.pl
> use strict;
> print while (<>) ;

Your code and my code, both, work for me under Win32.

Purl Gurl
--

TEST.PL
________

#!perl

use IPC::Open2;

$pid = open2(*INPUT, *OUTPUT, 'perl echo.pl');

print OUTPUT "Hello World\n";
close(OUTPUT);

$return = <INPUT>;
close (INPUT);

print $return;

ECHO.PL
_______

#!perl

print "I Am The Child: ";

print while (<>) ;

PRINTED RESULTS:
________________

I Am The Child: Hello World



Mon, 21 Nov 2005 22:12:37 GMT  
 open2 under windows


Quote:

> (snipped)

> > I am trying to get a bidirectional pipe working under both Unix and
> > Activestate 5.6.1 build 635. I thought my best bet would be
> > IPC::Open2, but so far it just hangs under Win2k. Here's the code.

> > # ipctest.pl
> > use strict;
> > use IPC::Open2;

> > my $pid;
> > my ($r, $w);
> > $pid = open2($r, $w, 'perl echo.pl');

> > print $w "Hello World\n";
> > close($w);

> > print STDOUT <$r>;

> > # echo.pl
> > use strict;
> > print while (<>) ;

> Your code and my code, both, work for me under Win32.

> Purl Gurl
> --

> TEST.PL
> ________

> #!perl

> use IPC::Open2;

> $pid = open2(*INPUT, *OUTPUT, 'perl echo.pl');

> print OUTPUT "Hello World\n";
> close(OUTPUT);

> $return = <INPUT>;
> close (INPUT);

> print $return;

> ECHO.PL
> _______

> #!perl

> print "I Am The Child: ";

> print while (<>) ;

> PRINTED RESULTS:
> ________________

> I Am The Child: Hello World

I get the same as Alan regarding his code - for both perl 5.6.1 and 5.8.0 on
Win2k.
Purl Gurl's code works fine for me - but only if $| is set to '1' in
echo.pl. Otherwise it still hangs.

With Alan's code, if I simply 'print STDOUT $r;' (or just 'print $r;') then
it no longer hangs - irrespective of whether $| has been set to '1'. But it
prints GLOB(0x1aa8db8). Not sure where to take it from there.

Cheers,
Rob



Tue, 22 Nov 2005 06:31:32 GMT  
 open2 under windows


Quote:
> Hello and tia:

>   I am trying to get a bidirectional pipe working under both Unix and
> Activestate 5.6.1 build 635. I thought my best bet would be
> IPC::Open2, but so far it just hangs under Win2k. Here's the code.

> # ipctest.pl
> use strict;
> use IPC::Open2;

> my $pid;
> my ($r, $w);
> $pid = open2($r, $w, 'perl echo.pl');

> print $w "Hello World\n";
> close($w);

># print STDOUT <$r>;

my $c = <$r>;
chomp($c);
print $c, "\n";

Quote:

> # echo.pl
> use strict;
$| = 1;
> print while (<>) ;

>   Thanks for the help.

>   -Alan

With the above changes it now works for me on Win2k. (Note that *both* files
have been amended.)

Fix supplied by $Bill Luebkert (on another forum).

Cheers,
Rob



Tue, 22 Nov 2005 13:54:29 GMT  
 open2 under windows
Thanks everyone:

  To follow up, I finally got IPC::Open2 to work under Unix and Win,
so thanks. I'd say the key was using $| in the child process to force
flushing. However, it's still a little unsatisfying because there may
be programs I want to talk to that don't flush. Would I be sol then?

  -Alan



Sat, 26 Nov 2005 23:09:23 GMT  
 open2 under windows

Quote:

> Thanks everyone:
>   To follow up, I finally got IPC::Open2 to work under Unix and Win,
> so thanks. I'd say the key was using $| in the child process to force
> flushing. However, it's still a little unsatisfying because there may
> be programs I want to talk to that don't flush. Would I be sol then?

Yup.  That's the whole reason behind all the warnings in Open2.

% perldoc IPC::Open2
[snip...]
    This whole affair is quite dangerous, as you may block forever.  It
    assumes it's going to talke to something like bc, both writing to it and
    reading from it. This is presumably safe because you "know" that
    commands like bc will read a line at a time and output a line ata time.
    Programs like sort that read their entire input stream first, however,
    are quite apt to cause deadlock.

    The big problem with this approach is that if you don't have control
    over source code being run in the child process, you can't control what
    it does with pipe buffering. Thus you can't just open a pipe to "cat -v"
    and continually read and write a line from it.

You may have noted that your child program behaved differently based on
whether it thought the output was to a pipe or to a tty.  Other
processes may do the same.  The POD goes on to talk about IO::Pty and
Expect, because they may provide a PTY to a process, so that its
behavior may be set to flush after a line.

--

Unix System Administrator                    Taos - The SysAdmin Company
Got some Dr Pepper?                           San Francisco, CA bay area
         < This line left intentionally blank to confuse you. >



Sat, 26 Nov 2005 23:30:17 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. IPC::Open2 on Windows NT

2. Fork, IPC:Open2, Windows?

3. IPC::Open2::open2() -- How to pass strings stdin, stdout?

4. open2 or open3 examples

5. open2/RD-WR pipe

6. open2 fails with perl5 on HP-UX??

7. ANNOUNCE: IPC open2/open3 emulation module for Win32

8. Remote launching (open2?)

9. open2 crashes in perl5b3

10. writing to a io handle using open2

11. IPC::open2()

12. IPC::Open2 and user ID

 

 
Powered by phpBB® Forum Software