#!/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); }