Link to home
Start Free TrialLog in
Avatar of ecuador
ecuador

asked on

Perl scripts & Open2

I casually use open2 when I want to pipe to/from various programs. However, when I tried to read/write to one of my perl scripts, it failed to work and it is taking me too long to figure it out, so I am asking you guys. I am attaching a very simple example that I expect to work, but it does not. Running test1.pl does nothing, the script hangs at my $r=<READ>. What am I doing wrong?
test1.pl
 
#!/usr/bin/perl
 
use IPC::Open2;
my $pid=open2 (\*READ, \*WRITE, "./test2.pl");
binmode WRITE, ":utf8";
foreach (qw(one two three)) {
    print WRITE "$_\n";
    my $r=<READ>;
    print "+$r+\n"
}
 
test2.pl
 
#!/usr/bin/perl
 
while (<STDIN>) {
    print "*$_*\n";
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ecuador
ecuador

ASKER

Simple... thanks!
I wonder why this is not considered worth mentionig in the open2 man pages...
perldoc IPC::Open2
says
       This whole affair is quite dangerous, as you may block forever.  It
       assumes it's going to talk 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 at a
       time.  Programs like sort that read their entire input stream first,
       however, are quite apt to cause deadlock.