-
Notifications
You must be signed in to change notification settings - Fork 2
/
net_curl_easy.pl
executable file
·30 lines (24 loc) · 1.01 KB
/
net_curl_easy.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/perl
use strict;
use warnings;
use Net::Curl::Easy qw[:constants];
$|=1;
my $curl = Net::Curl::Easy->new;
$curl->setopt(CURLOPT_HEADER,1);
$curl->setopt(CURLOPT_URL, 'http://www.cpan.org/src/5.0/perl-5.18.2.tar.gz');
$curl->setopt(CURLOPT_NOPROGRESS,0);
$curl->setopt(CURLOPT_PROGRESSFUNCTION, sub { my ( $easy, $dltotal, $dlnow, $ultotal, $ulnow, $uservar ) = @_; print join(' ', @_[1..4]), "\n"; return 0; } );
# A filehandle, reference to a scalar or reference to a typeglob can be used here.
open my $fh, '>', 'perl-5.18.2.tar.gz' or die "$!\n";
$curl->setopt(CURLOPT_WRITEDATA,$fh);
# Starts the actual request
my $retcode = $curl->perform;
# Looking at the results...
if ($retcode == 0) {
print("Transfer went ok\n");
my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
# judge result and next action based on $response_code
} else {
# Error code, type of error, error message
print("An error happened: $retcode ".$curl->strerror($retcode)." ".$curl->errbuf."\n");
}