I agree with ikegami that select is necessary.  Neither of the snippets above will quite work as written, however.

In the second one there's a missing comma in the my @cmd = ( ... ) line.

In the first one, $buf is never defined, and even if it were, you'd be overwriting it with the sysread call.    (It's also not necessary to use the OFFSET argument to sysread, since you can't perform a seek on STDOUT or STDERR).

You're also (in the first snippet) not getting anything written to STDERR, which puzzled me for a while, until I re-read the IPC::Open documentation more closely:

If ERRFH is false, or the same file descriptor as RDRFH, then STDO +UT and STDERR of the child are on the same filehandle.

Here's a suggested rewrite, using a subroutine run_command.  You give it a single argument which is the command to run, and it returns two list-references; the first is the lines of STDOUT, the second the lines of STDERR:

#!/usr/bin/perl -w # Strict use strict; use warnings; # Libraries use IO::File; use IO::Select; use IPC::Open3 qw/ open3 /; # Constants use constant BLOCK_SIZE => 4096; # User-defined my $module = "server"; my $OLD = "1.41"; my $NEW = "1.42"; my $cmd = "cvs rlog -r${OLD}::${NEW} -SN $module"; my $outbuf = ""; my $errbuf = ""; #################### ### Main program ### #################### my ($pout, $perr) = run_command($cmd); # Verify data print "\n[STDOUT]\n"; map { print "\e[102m$_\e[m\n" } @$pout; print "\n[STDERR]\n"; map { print "\e[101m$_\e[m\n" } @$perr; # Now do whatever you want to with the data $pout and $perr ... ################### ### Subroutines ### ################### sub run_command { my ($cmd) = @_; my $errfh = IO::File::new(); my $pid = open3(my $infh, my $outfh, $errfh, $cmd); my $r_sel = IO::Select->new($outfh, $errfh); my $pfh = { $outfh => [ ], $errfh => [ ] }; while ($r_sel->handles()) { my @can_read = $r_sel->can_read(0); foreach my $fh (@can_read) { my $pbuf = $pfh->{$fh}; my $rv = sysread($fh, my $text, BLOCK_SIZE); (defined $rv) or die "Failed cmd '$cmd' ($!)\n"; $rv or $r_sel->remove($fh); push @$pbuf, $text; } } waitpid($pid, 0); my $pout = [ split(/\n/, join("", @{$pfh->{$outfh}})) ]; my $perr = [ split(/\n/, join("", @{$pfh->{$errfh}})) ]; return ($pout, $perr); }

s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Re^3: Parsing STDERR and STDOUT at the same time by liverpole
in thread Parsing STDERR and STDOUT at the same time by qazwart

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.