Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Your skill will accomplish
what the force of many cannot

communicating with another program

by twotone (Beadle)
on Nov 30, 2005 at 08:33 UTC ( #512883=perlquestion: print w/ replies, xml ) Need Help??
twotone has asked for the wisdom of the Perl Monks concerning the following question:

I am using chapter 16 from the Perl Cookbook to try to figure out how to pass information to another program, then get results back from that program. I am using two perl scripts and

Eventually I will be communicating with a "someprogram.exe" written and compiled in fox pro by another programmer. For now, I'm using the two perl scripts to get it figured out. (both programs will be on the same machine)

So far, I can activate the script and get info back from it, but I can't successfully send info from to

Here are the scripts:
#!/usr/bin/perl -w use IPC::Open2; use IO::Handle; use strict; my $output = ''; my $program = ''; my ($infh,$outfh) = (IO::Handle->new, IO::Handle->new); open2($infh, $outfh, ($program)); print $outfh "The rain in Spain stays mainly on the plain.\n"; while (<$infh>) { $output .= $_; } close($outfh); close($infh); print $output; exit;
#!/usr/bin/perl -w use strict; my $input = ''; my $error = "We received nothing!"; while (my $line = <STDIN>) { $input .= $line; } if ($input ne '') { print "We received input:\n"; print $input; } else { print $error; } exit;

I run and all I get back is "We received nothing!" from

Any ideas what I'm doing wrong?

WinXP, ActiveState Perl 5.6.1

Comment on communicating with another program
Select or Download Code
Re: communicating with another program
by Perl Mouse (Chaplain) on Nov 30, 2005 at 10:00 UTC
    It's a deadlock situation. test1 prints a line, then waits for input from test2. At the same moment, test2 reads the line from test1, and then waits for more, until it receives end of file. But you don't close the output handle in test1 until you've seen end-of-file from test2. Both programs are waiting for input from the other, and they'll wait till they see end-of-file. Which is never going to happen.

    Close your output handle in test1 before reading input from test2 and it should work (although I didn't test it, there may be more bugs lurking).

    And it can't hurt to check the return value of open2.

    Perl --((8:>*
      Thanks, I should check the return value of open2. The thing I'm confused about with open2 is that according to the Perl Cookbook, it is supposed to let me both read from and write to a program. Hmmm, back to the drawing board... Gotta work on it a bit more to figure this out.
Re: communicating with another program
by Delusional (Beadle) on Nov 30, 2005 at 11:07 UTC
    Scalled down to the minimun. Only tested once to be sure it 'runs':
    my $returned = ` aaa bbb ccc`; print "Got \"$returned\" from\n"; exit;
    foreach $line (@ARGV) { $input .= $line; } if ($input ne '') { print "We received input:\n$input"; } else { print "Error, no input received!"; } exit;
      That does exactly what I need and is very simple. Thanks a bunch. :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://512883]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-04-21 03:46 GMT
Find Nodes?
    Voting Booth?

    April first is:

    Results (490 votes), past polls