I think your main problems is that select does not interact well with buffered I/O, which you use with <READERR> and <READOUT>. You should really use sysread and do a split /\n/ at the very end.

Here's how I might do it, using IPC::Open3 and IO::Select. You should add the waitpid/close from your example:

#!perl -w use IPC::Open3; use IO::Select; use Symbol qw(gensym); use strict; my $cmd = shift; my $g_timeout = shift || 5; my $g_maxlines = shift || 10; my @output; my @err; $| = 1; runcmd(\$cmd, \@output, \@err); print "output = \n"; print " $_\n" for @output; print "error = \n"; print " $_\n" for @err; sub runcmd { my ($cmdref, $outref, $errref) = @_; my ($childin, $childout, $childerr); $childerr = gensym; my $pid = open3($childin, $childout, $childerr, $cmd) or die "Cannot run cmd '$cmd': $!\n"; my $select = IO::Select->new or die "Cannot create select object: $! +\n"; my @hold_output; for (($childout, $childerr)) { $select->add($_); $hold_output[fileno($_)] = [0, 0, ""]; # eof, lines, buffer; } $@ = undef; eval { local $SIG{ALRM} = sub { die "alarm\n" }; my $deadline = $g_timeout + time; my $g_stop = 0; alarm($g_timeout + 1); while (!$g_stop && $select->count > 0) { $! = 0; my @ready = $select->can_read($deadline - time); if (!@ready) { $g_stop = 1; } for my $handle (@ready) { my $fno = fileno($handle); my $line; my $bytesread = sysread $handle, $line, 1024; if ($bytesread) { $hold_output[$fno][2] .= $line; $hold_output[$fno][1] += $line =~ y/\n/\n/; if ($hold_output[$fno][1] >= $g_maxlines) { $select->remove($handle); } } elsif ($!) { die "$!"; } else { $hold_output[$fno][0] = 1; #EOF $select->remove($handle); } } } alarm(0); }; if ($@) { # print STDERR "\$\@ = $@\n"; die unless $@ eq "alarm\n"; } # Note: lines may exceed $g_maxlines because of buffering # of output in the child process @$outref = split /\n/, $hold_output[fileno($childout)][2]; @$errref = split /\n/, $hold_output[fileno($childerr)][2]; }

In reply to Re: Redirecting stdout/stderr to pipe by Thelonius
in thread Redirecting stdout/stderr to pipe by 0xbeef

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.