Term::ProgressBar problems with multiple calls

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

This is a multi-part message in MIME format.
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Hash: SHA1

Hi there!

I am a newbie when it comes to posting to newsgroups, so please be
gentle! I posted this too c.l.p.modules, but maybe I just cannot write
perl-code so it fits in here.
Attached is a small sample script. In line 27 I build an array with
two urls.
I want to download those in a loop. This is what I get

$ ./progress_broken_sample.pl
Getting Trailers from 2 Trailerpages
Getting: http://nds2.photos.nokia.com/press/photo/phones/zip/6260_06.zip
Downloading 6260_06.zip
Answer: ""
~ ( 0.85MB )...
~  0%

]ETA ------
~  1%

]0m00s Left
........................ (more of that same till:) ....................
(nothing to do)
Getting: http://nds2.photos.nokia.com/press/photo/phones/zip/6260_02.zip
Downloading 6260_02.zip
Answer: ""
~ ( 1.58MB )...
(nothing to

(nothing to do)

Now, the download seems to work, but the progress-bar does not
initialize correctly.
What I normally do with this script is download movie trailers from
apple http://sourceforge.net/projects/downlets /
So for now I always need to call the script like this:

for i in
~ do
~  ./downlets/downlet_apple.pl -u $i

But that way the whole automatic I built into the script is useless.
Can anybody help me. I probably did something terribly wrong when
calling term::progress. I tried looking in the code of the module
itself, but I didn't find, where the progress is saved. Maybe it needs
to be reset somehow, before reusing it in the loop..


Version: GnuPG v1.2.5 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org


Content-Type: text/plain;
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;

use strict;

my $debug=1;
my $ask;

# For immediate output of progress bar

# LWP UserAgent is used for downloading the HTML Files from the web
# make sure we have the modules we need, else die peacefully.
# perl -e "use CPAN; install Bundle::LWP;"
eval("use LWP 5.6.9;"); die "[err] LWP 5.6.9 or greater required.\n" if $@;

# Progress Bar
eval("use Term::ProgressBar;"); # prevent word-wrapping.
die "[err] Term::ProgressBar not installed. Use \"perl -e \"use CPAN; install
Term::ProgressBar;\"\" to install \n" if $@;

use File::Basename;

# Start a new instance from UserAgent
my $ua = LWP::UserAgent->new;
# Set User Agent to iTunes
$ua->agent('User-Agent: iTunes/4.6 (Macintosh; U; PPC Mac OS X 10.3)');

# We save the trailerpages ../large.html or the like in this array
my @trailerpages = (
"http://nds2.photos.nokia.com/press/photo/phones/zip/6260_06.zip ",
"http://nds2.photos.nokia.com/press/photo/phones/zip/6260_02.zip ");

print "Getting Trailers from ". ($#trailerpages + 1) ." Trailerpages\n" if

foreach (@trailerpages)
 printf "Getting: %s\n", $_ if $debug;

  my $url = $_;

 my $response;
 my $result;
 my $remote_headers;
 my $total_size;
 my $progress;
 my $final_data_length;
 my $next_update = 0; # reduce ProgressBar use.
  if( -e basename($url)) {
   print "Trailer ". substr(basename($url), 0, 40) ." already exists\n" if
  } else {
  print "Downloading ", substr(basename($url), 0, 40);
  my $answer = "";
  if($ask) {
   print " OK? (Y/n): ";
   $answer = <STDIN>;
  print "\nAnswer: \"$answer\"\n" if $debug;

  open (MOVFILE, "> " . basename($url)) || die $!;
  if(! ($answer =~ /^n/i) ) {  
   # create a new useragent and download the actual URL.
   # all the data gets thrown into $final_data, which
   # the callback subroutine appends to.
   #my $ua = LWP::UserAgent->new( );
   $result = $ua->head($url);
   $remote_headers = $result->headers;
   $total_size = $remote_headers->content_length;
   printf( " ( %.2fMB )... \n", $total_size/1024/1024 );
   # initialize our progress bar.
   $final_data_length = undef;
   $next_update = 0;
   $progress = Term::ProgressBar->new({count => $total_size, ETA => 'linear', fh
 => \*STDOUT});
   $progress->minor(0); # turns off the floating asterisks.
   $progress->max_update_rate(1); # only relevant when ETA is used.
   $response = $ua->get($url, ':content_cb' => \&callback, );
   # top off the progress bar.

     # per chunk.
     sub callback {
      my ($data, $response, $protocol) = @_;
      print MOVFILE $data;
      $final_data_length += length($data);
      #print "\n" . $response. " " . length($data) . "\n";
      # reduce usage, as per example 3 in POD.
      $next_update = $progress->update($final_data_length)
         if $final_data_length >= $next_update;

   #$progress = '';

  } else {
   print "Empty File created\n";
  close (MOVFILE);

 }# $mov != 0


Site Timeline