zsdc wrote:<snip>
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.
Try running this program:
#!/usr/bin/perl
use strict; use warnings;
$| = 1; my $total = `make -n | wc -l`; my $count = 0; open MAKE, "make 2>/dev/null |" or die "Can't open MAKE pipe"; while (<MAKE>) { $count++; my $percent = int(($count / $total) * 100); print "$percent%\r"; } print "Done.\n"; __END__
Does it work? Keep in mind that unless every command in your Makefile ends with >/dev/null you will get values greater than 100%.
Ok, so it was the 'foreach' causing my problem the whole time. Here is my current code:
#!/usr/bin/perl
use strict; use warnings;
$| = 1; my $total = `make -n | wc -l`; my $count = 0; open MAKE, "make |" or die "Can't open MAKE pipe"; while (<MAKE>) { $count++; my $percent = int(($count / $total) * 100); print "\r${percent}% "; } print "\n";
This code works just fine providing that the Makefile commands don't output anything. Although, most Makefiles don't output anything except the commands being executed (which is covered by 'make -n' and compile errors. My next step is to add a "scrollback buffer" in order to display compile errors. I was thinking about something like:
Would that work to keep the last 15 lines of output?
The following works for the scrollback buffer:
#!/usr/bin/perl
use strict; use warnings;
$| = 1; my $total = `make -n | wc -l`; my $count = 0; my (@buffer, $line); open MAKE, "make |" or die "Can't open MAKE pipe"; while (<MAKE>) { $count++; $line = "$count - $_"; if($#buffer < 14) { push @buffer, $line; } else { for(my $tmp=1;$tmp<15;$tmp++) { $buffer[($tmp-1)] = $buffer[$tmp]; } $buffer[14] = $line; } my $percent = int(($count / $total) * 100); print "\r${percent}% "; } print "\n"; foreach $line (@buffer) { print $line; }
Now, my question is how do I get the return value of 'make' once the process terminates with the above code? I want the program to dump the "buffer" when 'make' returns an error code.
-- 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>