#!/usr/bin/perl -w use strict; use Term::ProgressBar; use URI; use LWP::UserAgent; my $ua = LWP::UserAgent->new; while (@ARGV) { my $url = shift; print "$url:\n"; my $uri = URI->new($url); my $path = $uri->path; $path =~ s{.*/}{}; $path = "download" unless length $path; $path = "X$path" while -e $path; open my $outhandle, ">", $path or die "Cannot create $path: $!"; my $bar = Term::ProgressBar->new({ name => 'Download', count => 1024, ETA => 'linear'}); my $output = 0; my $target_is_set = 0; my $next_so_far = 0; $ua->get ($url, ":content_cb" => sub { my ($chunk, $response, $protocol) = @_; unless ($target_is_set) { if (my $cl = $response->content_length) { $bar->target($cl); $target_is_set = 1; } else { $bar->target($output + 2 * length $chunk); } } $output += length $chunk; print {$outhandle} $chunk; if ($output >= $next_so_far) { $next_so_far = $bar->update($output); } }); $bar->target($output); $bar->update($output); }