Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Capturing STDOUT and STDERR of system command, with pure perl?

by zentara (Archbishop)
on Aug 24, 2007 at 14:46 UTC ( [id://634880]=note: print w/replies, xml ) Need Help??


in reply to Capturing STDOUT and STDERR of system command, with pure perl?

If you are sticking to *nix type systems, IPC::Open3 should work. Here is a general example using a shell, but you could open a new IPC::Open3 for each command. Of course, there is a file descriptor, but so does IPC::Run.
#!/usr/bin/perl use warnings; use strict; use IPC::Open3; use IO::Select; my $pid = open3(\*WRITE, \*READ,\*ERROR,"/bin/bash"); my $sel = new IO::Select(); $sel->add(\*READ); $sel->add(\*ERROR); my($error,$answer)=('',''); while(1){ print "Enter command\n"; chomp(my $query = <STDIN>); #send query to bash print WRITE "$query\n"; foreach my $h ($sel->can_read) { my $buf = ''; if ($h eq \*ERROR) { sysread(ERROR,$buf,4096); if($buf){print "ERROR-> $buf\n"} } else { sysread(READ,$buf,4096); if($buf){print "$query = $buf\n"} } } } waitpid($pid, 1); # zombie prevention

I'm not really a human, but I play one on earth. Cogito ergo sum a bum
  • Comment on Re: Capturing STDOUT and STDERR of system command, with pure perl?
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://634880]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-23 11:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found