Perl Progress BAR


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Progress BAR
# 1  
Old 01-06-2011
Perl Progress BAR

Looking for a solution to why my progress bar not working

My code
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
Code:
print STDOUT  "#";

# 2  
Old 01-06-2011
Does the bracket I highlighted in red close your for loop too soon?

Quote:
for my $pagenum (1 .. $pdf->numPages) {
...
DO LOT OF PROCESSING HERE
...
}
print STDOUT "#"; # THIS IS PROGRESS BAR one per page
&print_it;
}
# 3  
Old 01-06-2011
kind of ... below code

i checked with perl -cw ... says ..syntax OK

Code:
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 ...
# 4  
Old 01-06-2011
Before your first write to STDOUT and before selecting any other filehandle (if you do 'select' it somewhere) issue the following command:

Code:
 
$| = 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.

Code:
#!/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 ...";
}


Last edited by rwuerth; 01-06-2011 at 10:17 AM.. Reason: added my test code
# 5  
Old 01-06-2011
Yes worked as needed .... Thanks
Code:
$|  ----- 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 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Progress bar

Hi Experts; Im in the process of writing a shell script for enabling an IT operations to run archiving.We use netbackup. The script is complete, though there is one bit that i need help on. Im trying to have a progess bar for the procedure.I have gone through the man page of the command in... (5 Replies)
Discussion started by: maverick_here
5 Replies

2. Shell Programming and Scripting

Progress bar for cp

I'm trying to use this code to get a progress bar for cp: "Can you get cp to give a progress bar like wget?" But I'm getting these errors: stat: illegal option -- c usage: stat awk: division by zero input record number 1, file source line number 4 I'm using Mac OS X 10.6... (1 Reply)
Discussion started by: pcwiz
1 Replies

3. Programming

A progress bar in C

Hello, it's me again...:eek: I need to create a progress bar in C, but i have no idea on how to do it. i want it to output something like this: Progress: 58% But i can't get it to work. Could you please post an example progress bar written in ANSI C? Thanks (4 Replies)
Discussion started by: Zykl0n-B
4 Replies

4. Shell Programming and Scripting

Progress Bar in Perl for UNIX

Hi Programmers, I just wrote a small script to print the percent complete. This maybe useful for someone! #!/usr/local/bin/perl # Total from which percentage will be calculated $cnt = 16; $|=1; for($i=0;$i<$cnt;$i++) { # Calculate Percentage $percent = ($i/$cnt)*100; (13 Replies)
Discussion started by: hifake
13 Replies

5. Shell Programming and Scripting

progress bar

hi all, in shell script (ksh), how do i write a progress bar ?? i have a script which searches files and while its searching i am currently printing out "." and if it finds what its searching for the script prints out the name of the file e.g .................. firstFile.txt... (2 Replies)
Discussion started by: cesarNZ
2 Replies

6. UNIX for Advanced & Expert Users

how to have a cp progress bar?

Hi all, This is a reformed post to my earlier ones!!!!!! I would like to know how to include a progress bar while using the cp... I am copying a few huge files from cdrom but am unable to figure out ,how to give a progress bar!!!!! I checked out other sites as well,but the issue here is... (1 Reply)
Discussion started by: wrapster
1 Replies

7. Shell Programming and Scripting

perl progress bar dialog

Hello, In perl how to show the percentage download when we download something wget ... For example when we wget some software instead of showing the following ...... ============================================ # wget... (4 Replies)
Discussion started by: fed.linuxgossip
4 Replies

8. UNIX for Advanced & Expert Users

progress bar

Hi all, I want to print # like that in a progress bar.. For e.g We can notice that during installation ... but,how to do that? Thnx, sakthi. (4 Replies)
Discussion started by: sakthi.abdullah
4 Replies

9. Shell Programming and Scripting

progress bar

i am trying to write a script where in it will connect to remote servers and execute remote scripts to fetch some data and ftp it back to a main server. i would like to add a script where it will show some sort of status bar until such time that the expected files have been recieved. something... (3 Replies)
Discussion started by: inquirer
3 Replies
Login or Register to Ask a Question