splitting stderr/stdout without using file

B

billy

I need to capture stdout and stderr but without using files for the
capture. This script will be part of a logging function that is called
from make. Make is processing as many jobs as the system loading can
handle. This script may be called 100k+ times and hundreds of times
simultanously, so writing to files is out of the question. I already
have a version in sh but it has to create several processes to
accomplich it's task. I am out to gain all the spedd I ppssibly can
..1 seconds is a lot of time saved over 100k calls.

I currently have a function System that I have used to capture both
stdout and stderr.
I need to modify this;
Here's what I have.

test code x.pl :
#!/usr/local/bin/perl
print STDOUT "$0 stdout\n";
print STDERR "$0 stderr\n";
exit 0;

main code :
#!/usr/local/bin/perl -w
use strict;
use warnings;
my ($stat,$ret) = System("x.pl");
print "ret = '$ret'\n";
sub System
{
my( $command ) = @_;
my $stat = undef;
my $ret = undef;
if (open PIPE , "$command 2>&1 |") {
while (<PIPE>) { $ret .= $_; }
close PIPE;
}
else {
Unknown("Failed to open pipe for '$command'!\n",1,1);
}
$stat = $? >> 8;
return ($stat,$ret);
}
 
B

billy

I have it! :)
sub System
{
my( $command ) = @_;
my $stat = undef;
local (*IN, *OUT, *ERR);
my $pid = IPC::Open3::eek:pen3(*IN, *OUT, *ERR,$command);
my $out = join ' ' , <OUT>;
my $err = join ' ' , <ERR>;
waitpid($pid,0);
$stat = $? >> 8;
close OUT;
close ERR;
return ($stat,$out,$err,$pid);
}
 
X

xhoster

billy said:
I have it! :)
sub System
{
my( $command ) = @_;
my $stat = undef;
local (*IN, *OUT, *ERR);
my $pid = IPC::Open3::eek:pen3(*IN, *OUT, *ERR,$command);
my $out = join ' ' , <OUT>;
my $err = join ' ' , <ERR>;

That will deadlock if the process prints more than one pipe-buffer worth of
data to its stderr (the parent's ERR). You should put OUT and ERR into
IO::Select and use that to read without deadlocking.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top