Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"


by pschoonveld (Pilgrim)
on May 19, 2000 at 21:40 UTC ( #13292=perlquestion: print w/ replies, xml ) Need Help??
pschoonveld has asked for the wisdom of the Perl Monks concerning the following question:

I need to execute a program, write some data to its stdin and then read its stdout. A little research has yieled that open FILE, "| $prog |"; does not work for some less than obvious reasons.

Gladly, perl comes with IPC::Open2, which allows you to direct the STDIN and STDOUT of an executed program and let your script control them. But, for some reason my code isn't working. Here is is:

#!/usr/bin/perl use IPC::Open2; print open2(\*BOIN, \*BOOUT, "cat"); print BOIN "proclist\n"; print BOIN "quit\n"; close BOIN; while (<BOOUT>) { print; } close BOOUT;
Prints nothing. Any thoughts?

Comment on IPC::Open2
Select or Download Code
Replies are listed 'Best First'.
Re: IPC::Open2
by btrott (Parson) on May 19, 2000 at 21:48 UTC
    Because you've switched BOIN and BOOUT.

    You need to write to BOOUT and read from BOIN--you were doing the opposite. It should read:

    #!/usr/bin/perl -w use strict; use IPC::Open2; print open2(\*BOIN, \*BOOUT, "cat"); print BOOUT "proclist\n"; print BOOUT "quit\n"; close BOOUT; while (<BOIN>) { print; } close BOIN;
    If you'd had warnings on in your original, you would have received this warning:
    Filehandle main::BOIN opened only for input at line 8. Filehandle main::BOIN opened only for input at line 9.
    By the way, as perlipc warns, watch out for Unix buffering. In this case (using cat) you might want to use the -u flag for unbuffered output. In your case, it doesn't matter much, since you close the output filehandle. But in general, it's something to think about.
Re: IPC::Open2
by pschoonveld (Pilgrim) on May 19, 2000 at 21:53 UTC
    Damn, I'm stupid. RTFM for me. Thanks.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2015-11-29 14:51 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (751 votes), past polls