Andrew Gaffney wrote:
zsdc wrote:

Andrew Gaffney wrote:

I tried that and it still spits out all the output at the end. Here's my current code:

#!/usr/bin/perl

use strict;
use warnings;

$| = 1;
my $total = `make -n | wc -l`;
print "$total\n";
my ($count, $line);
open MAKE, "make |" or die "Can't open MAKE pipe";
foreach (<MAKE>) {
  $count++;
  my $percent = int(($count / $total) * 100);
#  print "${percent}...";
  print $_;
}



First of all, using foreach will _always_ wait for all of the data before doing anything else, no matter what, so first change:


foreach (<MAKE>) {

to:

while (<MAKE>) {

and then try to find out if maybe it is indeed being buffered, though $| won't help you here, as it sets autoflushing on output, not input.

Another thing is that "make" is probably going to print much more lines than "make -n" because "make -n" only prints the commands to be executed while those commands themselves are likely to produce some output as well, unless all of them are run with >/dev/null 2>&1 at the end.


What approach would you recommend?

I figured out another approach. Instead of counting lines, it counts 'make \[\d+\]: Leaving' and just calculates the percent on how many directories 'make' has been in. I still use the old method for source trees that don't have multiple directories. It seems to work.


#!/usr/bin/perl

use strict;
use warnings;

$| = 1;
my $count = 0;
my $makeargs = "";
foreach (shift) {
  $makeargs .= " $_";
}
my (@buffer, $line, $total, $makeout, @makedirs);
my $onedir = 1;
my $dirpos = 0;
$makeout = `make -n ${makeargs}`;
if($makeout =~ /^make\[\d+\]: Entering/m) {
  $onedir = 0;
  while($makeout =~ /^make\[\d+\]: Leaving directory `(.+)'$/cgm) {
    push @makedirs, $1;
  }
}
if($onedir) {
  $total = `echo ${makeout} | wc -l`;
}
open MAKE, "make ${makeargs} |" or die "Can't open MAKE pipe";
while (<MAKE>) {
  my $percent;
  $count++;
  $line = "$_";
  if($#buffer < 14) {
    push @buffer, $line;
  } else {
    for(my $tmp=1;$tmp<15;$tmp++) {
      $buffer[($tmp-1)] = $buffer[$tmp];
    }
    $buffer[14] = $line;
  }
  if($onedir) {
    $percent = int(($count / $total) * 100);
  } else {
    chomp($line);
    if($line =~ /^make\[\d+\]: Leaving directory `$makedirs[$dirpos]'$/) {
      $dirpos++;
    }
    $percent = int(($dirpos / ($#makedirs + 1)) * 100);
  }
  print "\r${percent}% ";
}
print "\n";

--
Andrew Gaffney
Network Administrator
Skyline Aeronautics, LLC.
636-357-1548


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to