Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl大容量直接下载到文件(以避免RAM过载)_Perl_Download_Out Of Memory_Large Files - Fatal编程技术网

Perl大容量直接下载到文件(以避免RAM过载)

Perl大容量直接下载到文件(以避免RAM过载),perl,download,out-of-memory,large-files,Perl,Download,Out Of Memory,Large Files,我正试图将一个大小约为1GB的文件下载到一个内存为1GB的服务器上,因此如果我试图将其下载到一个变量(下面的代码)中,操作系统会由于RAM过载而终止进程 require LWP::UserAgent; my $ua = LWP::UserAgent->new; $ua->timeout(3600); $ua->env_proxy; my $response = $ua->get('http://example.com/largefile.xml'); if ($re

我正试图将一个大小约为1GB的文件下载到一个内存为1GB的服务器上,因此如果我试图将其下载到一个变量(下面的代码)中,操作系统会由于RAM过载而终止进程

require LWP::UserAgent;

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

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

if ($response->is_success) {
    print "File downloaded\n";
}
else {
    die $response->status_line;
}
我认为唯一可行的方法是使用
系统(“wget…”)
(或curl或类似的东西),但我确信有一种正确的方法可以直接使用Perl来实现

您知道有什么方法或选项可以不使用系统调用直接将其下载到文件中吗?

是的。查看的
get
方法:

$ua->get($url,$field\u name=>$value,…)

以“:”开头的字段名称是特殊的。这些将不会初始化请求的头,但将确定如何处理响应内容。可以识别以下特殊字段名:

如果$filename随
:content\u file
选项一起提供,则响应内容将保存在此处而不是响应对象中

注意-它可能无法很好地处理文件系统错误,所以请检查是否可以写入该位置。(权限、目录存在等)

:content_file   => $filename