Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

IPC::Open2

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
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 foo.pl line 8. Filehandle main::BOIN opened only for input at foo.pl 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2014-07-10 18:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (215 votes), past polls