Hi qazwart,

The following code is based on an example in the documentation from perlfaq8 (under the question "How can I capture STDERR from an external command?"):

#!/usr/bin/perl -w # Strict use strict; use warnings; # Libraries use IPC::Open3; use Symbol qw(gensym); use IO::File; #################### ### Main program ### #################### # User-defined my $module = "mymodule"; my $OLD = "1.41"; my $NEW = "1.42"; my $cmd = "cvs rlog -r${OLD}::${NEW} -SN $module"; local *CATCHOUT = IO::File->new_tmpfile; local *CATCHERR = IO::File->new_tmpfile; my $pid = open3(gensym(), ">&CATCHOUT", ">&CATCHERR", $cmd); waitpid($pid, 0); seek(\*CATCHOUT, 0, 0); seek(\*CATCHERR, 0, 0); process_stdout(*CATCHOUT); process_stderr(*CATCHERR); sub process_stdout { my ($outfh) = @_; while (my $line = <$outfh>) { # Handle $line from STDOUT chomp $line; print "\e[102m$line\e[m\n"; # Change this } } sub process_stderr { my ($errfh) = @_; while (my $line = <$errfh>) { # Handle $line from STDERR chomp $line; print "\e[101m$line\e[m\n"; # Change this } }

I even tested it on a cvs module locally to make sure it handles both STDOUT and STDERR from the cvs rlog command correctly.

At the moment, it's just displaying the lines it reads (in different colors; green for STDOUT, red for STDERR), but you can modify the 2 lines marked "Change this" above to suit your needs.


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

In reply to Re: 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.