?
Solved

Perl scripts & Open2

Posted on 2008-11-03
4
Medium Priority
?
1,446 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:ecuador
  • 3
4 Comments
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 22873021
#!/usr/bin/perl

$|=1;
while (<STDIN>) {
    print "*$_*\n";
}
0
 
LVL 85

Expert Comment

by:ozo
ID: 22873062
0
 

Author Comment

by:ecuador
ID: 22873534
Simple... thanks!
I wonder why this is not considered worth mentionig in the open2 man pages...
0
 
LVL 85

Expert Comment

by:ozo
ID: 22873552
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.

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question