Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-20 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found