in reply to Handling STDERR and STDIN in Perl
This is not easy stuff. Recipe 16.10 from the Perl Cookbook recommends using IPC::Open3, which brings fork into the picture. Here is Example 16.2 reprinted (i threw in a few minor adjustments). You should be able to wrap this in a sub and pass in the shell command, but please do RTFM the module docs before you just add this to production code, if any.
Hope this helps, and don't ++ me ... just buy the Cookbook. :)# cmd3sel - control all three of kids in, out, and error. use IPC::Open3; use IO::Select; my $cmd = "grep vt33 /none/such - /etc/termcap"; my $pid = open3(*CMD_IN, *CMD_OUT, *CMD_ERR, $cmd); $SIG{CHLD} = sub { warn "REAPER: status $? on $pid\n" if waitpid($pid, 0) > 0 }; print CMD_IN "This line has a vt33 lurking in it\n"; close(CMD_IN); my $selector = IO::Select->new(); $selector->add(*CMD_ERR, *CMD_OUT); while (my @ready = $selector->can_read) { foreach my $fh (@ready) { if (fileno($fh) == fileno(CMD_ERR)) {print "STDERR: ", scalar <CMD_ERR>} else {print "STDOUT: ", scalar <CMD_OUT>} $selector->remove($fh) if eof($fh); } } close(CMD_OUT); close(CMD_ERR);
UPDATE: have you looked into Expect?
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Handling STDERR and STDIN in Perl
by Anonymous Monk on May 10, 2004 at 05:55 UTC |