in reply to Parsing STDERR and STDOUT at the same time
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Parsing STDERR and STDOUT at the same time
by ikegami (Patriarch) on Feb 02, 2007 at 17:05 UTC | |
by liverpole (Monsignor) on Feb 02, 2007 at 19:18 UTC | |
by ikegami (Patriarch) on Feb 02, 2007 at 19:56 UTC | |
|
Re^2: Parsing STDERR and STDOUT at the same time
by qazwart (Scribe) on Feb 02, 2007 at 15:50 UTC |