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

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 test1.pl and test2.pl.

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 test2.pl script and get info back from it, but I can't successfully send info from test1.pl to test2.pl.

Here are the scripts:

test1.pl
#!/usr/bin/perl -w #test1.pl use IPC::Open2; use IO::Handle; use strict; my $output = ''; my $program = 'test2.pl'; 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;
test2.pl
#!/usr/bin/perl -w #test2.pl 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 test1.pl and all I get back is "We received nothing!" from test2.pl.

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':

    test1.pl
    my $returned = `test2.pl aaa bbb ccc`; print "Got \"$returned\" from test2.pl\n"; exit;

    test2.pl
    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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://512883]
Approved by Corion
help
Chatterbox?
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
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (490 votes), past polls