Looking for a solution to why my progress bar not working
My code
print "STARTED:\n ";
my $pdf = CAM::PDF->new('XYZ.pdf') or die $CAM::PDF::errstr;
print STDOUT "Processing File: ";
open (FILE, ">bill_data.txt") || die "Unable to save Bill_data.txt file";
for my $pagenum (1 .. $pdf->numPages) {
...
DO LOT OF PROCESSING HERE
...
}
print STDOUT "#"; # THIS IS PROGRESS BAR one per page
&print_it;
}
Close(FILE)
print "\t [ DONE ]\n"; # Just look nice once done
sub print_it{
print FILE "SOME STUFF ... based on other process..."
..
Around 20/30 print FILE command
..
}
Now when the program starts I get the message
STARTED:
later it does not show me any progress and then end of program all the #
done etc are printed.
Any thing wrong that I am doing ... I also tried does not work
print STDOUT "#";
Does the bracket I highlighted in red close your for loop too soon?
kind of ... below code
i checked with perl -cw ... says ..syntax OK
for my $pagenum (1 .. $pdf->numPages) {
my $pagetree = $pdf->getPageContentTree($pagenum) or die;
my @text = $pagetree->traverse('MyRenderer')->getTextBlocks;
for my $textblock (@text) {
&create_page($textblock->{str},$textblock->{left},$textblock->{bottom});
}
print STDOUT "#";
&print_it;
}
MyRender is a package defined in same code ...
Before your first write to STDOUT and before selecting any other filehandle (if you do 'select' it somewhere) issue the following command:
$| = 1;
This sets autoflush to "true" for STDOUT and the output will not be buffered.
This code (stripped down version of yours, without the pdf processing) worked as I think you want yours to work.
#!/usr/bin/perl
$| = 1;
print "STARTED:\n";
print STDOUT "Processing file: ";
open (FILE, ">bill_data.txt") || die "Can't open bill_data.txt";
for my $pagenum ( 1 .. 10 ) {
sleep(5); # for testing only. replaces other processing and embedded for loop.
print STDOUT "#";
&print_it;
}
close (FILE);
print "\t [DONE] \n";
sub print_it {
print FILE "SOME STUFF ... based on other process ...";
print FILE "SOME STUFF ... based on other process ...";
print FILE "SOME STUFF ... based on other process ...";
print FILE "SOME STUFF ... based on other process ...";
print FILE "SOME STUFF ... based on other process ...";
print FILE "SOME STUFF ... based on other process ...";
}
Yes worked as needed .... Thanks
$| ----- If set to nonzero, forces a flush of th currently selected stream after every write (Default value = 0)
But usually it was not needed. I could get it working.
Perl is stranger to me ... sometimes