in reply to Re: Parsing STDERR and STDOUT at the same time
in thread Parsing STDERR and STDOUT at the same time

That will deadlock. If either the STDOUT or the STDERR pipe fills up, waitpid will never return. That's why select is required. (Or in this case, can_read since we don't feed any input to cvs.)
#!/usr/bin/perl use strict; use warnings; use IO::Select qw( ); use IPC::Open3 qw( open3 ); use constant BLOCK_SIZE => 4096; sub process_stdout { my ($fh) = @_; local *_; # Protect caller's $_ while (<$fh>) { chomp; print("[out:$_]\n"); } } sub process_stderr { my ($fh) = @_; local *_; # Protect caller's $_ while (<$fh>) { chomp; print("[err:$_]\n"); } } { # User-defined my $module = "mymodule"; my $OLD = "1.41"; my $NEW = "1.42"; my $cmd = "cvs rlog -r${OLD}::${NEW} -SN $module"; my ($fh_cvs_in, $fh_cvs_out, $fh_cvs_err); my $pid = open3($fh_cvs_in, $fh_cvs_out, $fh_cvs_err, $cmd); my $r_sel = IO::Select->new($fh_cvs_out, $fh_cvs_err); my $cvs_out = ''; my $cvs_err = ''; while ($r_sel->handles()) { my @r = $r_sel->can_read(); foreach my $fh (@r) { if ($fh == $fh_cvs_out) { my $rv = sysread($fh, $buf, BLOCK_SIZE, length($buf)); if (not defined $rv) { die("Unable to communicate with CVS: $!\n"); } if (not $rv) { # End of file $r_sel->remove($fh_cvs_out); } } elsif ($fh == $fh_cvs_err) { my $rv = sysread($fh, $buf, BLOCK_SIZE, length($buf)); if (not defined $rv) { die("Unable to communicate with CVS: $!\n"); } if (not $rv) { # End of file $r_sel->remove($fh_cvs_err); } } } } waitpid($pid, 0); { # Requires Perl 5.8. # In Perl 5.6, use IO::Scalar # or work with $csv_out directly. use 5.008000; open(my $fh, '<', \$cvs_out); process_stdout($fh); } { # Requires Perl 5.8. # In Perl 5.6, use IO::Scalar # or work with $csv_err directly. use 5.008000; open(my $fh, '<', \$cvs_err); process_stderr($fh); } }

IPC::Run is designed to simplify this, but I have never used it.

use strict; use warnings; use IPC::Run qw( run ) ; sub process_stdout { my ($fh) = @_; local *_; # Protect caller's $_ while (<$fh>) { chomp; print("[out:$_]\n"); } } sub process_stderr { my ($fh) = @_; local *_; # Protect caller's $_ while (<$fh>) { chomp; print("[err:$_]\n"); } } { # User-defined my $module = "mymodule"; my $OLD = "1.41"; my $NEW = "1.42"; my @cmd = ( 'cvs', 'rlog', "-r${OLD}::${NEW}" '-SN', $module ); my $cvs_in = ''; my $cvs_out = ''; my $cvs_err = ''; run \@cmd, \$cvs_in, \$cvs_out, \$cvs_err or die("Unable to launch CVS\n"); { # Requires Perl 5.8. # In Perl 5.6, use IO::Scalar # or work with $csv_out directly. use 5.008000; open(my $fh, '<', \$cvs_out); process_stdout($fh); } { # Requires Perl 5.8. # In Perl 5.6, use IO::Scalar # or work with $csv_err directly. use 5.008000; open(my $fh, '<', \$cvs_err); process_stderr($fh); } }

Neither snippet has been tested.
Either or both snippets may need to trap SIGPIPE.

Replies are listed 'Best First'.
Re^3: Parsing STDERR and STDOUT at the same time
by liverpole (Monsignor) on Feb 02, 2007 at 19:18 UTC
    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$..$/
      Thanks, I wasn't in a position to test at the time.