
Opening a pipe to a command
|>This maybe a dumn question (can't see the wood for the trees and all that) but
|>I want to open up a pipe to a command and get back all it's output. Using
|>system() or backticks works as long as the command isn't interactive, for
|>example system("rlogin machine -l user") will hand over to STDIN for the
|>password, but I want that to come from the perl script and still get any
|>output from the command back to do whatever I want with.
|>
|> What I want to do is open(PIPE,"| command |"); which of course can't
|>be done. Perl should be able to do this in one form or another.
I just provided this snippet for somebody else....
---------------------------------------
pipe(INSTD, OUTSTD);
pipe(INERR, OUTERR);
if (fork) {
close(OUTSTD);
close(OUTERR);
wait;
while (<INSTD>) { print "OUT: $_\n"; }
while (<INERR>) { print "ERR: $_\n"; }
Quote:
} else {
close(STDOUT);
close(STDERR);
open(STDOUT, ">&OUTSTD");
select STDOUT; $| = 1;
open(STDERR, ">&OUTERR");
select STDERR; $| = 1;
Quote:
}
---------------------------------------
This example has been tested and works. You should be able to alter
it to do this.....
---------------------------------------
pipe(INSTD, OUTSTD);
pipe(INERR, OUTERR);
pipe(ININ, OUTIN);
if (fork) {
close(OUTSTD);
close(OUTERR);
close(ININ);
select OUTIN; $| = 1;
print OUTIN $input;
wait;
while (<INSTD>) { print "OUT: $_\n"; }
while (<INERR>) { print "ERR: $_\n"; }
Quote:
} else {
close(STDOUT);
close(STDERR);
close(STDIN);
open(STDOUT, ">&OUTSTD");
select STDOUT; $| = 1;
open(STDERR, ">&OUTERR");
select STDERR; $| = 1;
open(STDIN, "<&ININ");
Quote:
}
---------------------------------------
This creates pipes for stdin, stdout and stderr for the child
process. I haven't tested this version.
Lezz "JAPH" Giles