9

I'm downloading a large file directly to a file with Perl using LWP::UserAgent and the :content_file option.

This is a simplified example of my code:

require LWP::UserAgent;

my $ua = LWP::UserAgent->new;
$ua->timeout(3600);
$ua->env_proxy;

my $response = $ua->get(
    'http://example.com/largefile.xml',
    :content_file   => 'path/to/file/largefile.xml'
);

if ($response->is_success) {
    print "File downloaded\n";
}
else {
    die $response->status_line;
}

Is there any way to display the percentage of the download status? (or something similiar to wget output)

10% [===>                                  ]  65.120.154  527K/s 
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
nanocv
  • 2,227
  • 2
  • 14
  • 27

2 Answers2

10

From the documentation for the module.

$ua->show_progress

$ua->show_progress( $boolean )

Get/set a value indicating whether a progress bar should be displayed on the terminal as requests are processed. The default is FALSE.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • Also, take a look at source code of /bin/lwp-download of ActivePerl installation. – Kostia Shiian Nov 09 '16 at 20:10
  • 3
    @KostiaShiian: lwp-download isn't specific to ActivePerl. It's [available from CPAN](https://metacpan.org/pod/distribution/libwww-perl/bin/lwp-download). – Dave Cross Nov 09 '16 at 21:04
3

Dave has already answered your question but I would like to suggest below 2 modules.

Community
  • 1
  • 1
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
  • Thank you! I'll take a look at both of them aswell. – nanocv Nov 10 '16 at 07:28
  • LWP::UserAgent::ProgressBar provides a progress bar very much in line with what the OP had in mind. It's just not likely to be already installed, if that's an issue (eg, for portability) – Randall Mar 08 '18 at 21:19