简体   繁体   English

Perl和LWP:getstore()仅在不指定目录的情况下保存文件

[英]Perl & LWP: getstore() will only save a file if I don't specify a directory

I'm probably missing something simple but I have got getstore() running perfectly to save a ".png" file to my Windows PC as follows: 我可能缺少一些简单的东西,但我的getstore()运行得很好,可以将“ .png”文件保存到Windows PC中,如下所示:

getstore("$url","$filename");

That works a treat and downloads the file perfectly to the directory containing the Perl script. 这样可以有效地将文件完美下载到包含Perl脚本的目录中。 However if I try saving it to a subfolder of "newdir" nothing downloads: 但是,如果我尝试将其保存到“ newdir”的子文件夹中,则不会下载任何内容:

getstore("$url","newdir\\$filename");

Any idea where I'm going wrong here or how I could debug? 知道我在哪里出错或如何调试吗? I tried printing the directory and it looks good: 我尝试打印目录,看起来不错:

print "newdir\\$filename";

However there's nothing in "newdir" once the script runs. 但是,一旦脚本运行,“ newdir”中就没有任何内容。 Thanks in advance. 提前致谢。

The file is opened using open(my $fh, ">", $arg) , where $arg contains the value you pass. 使用open(my $fh, ">", $arg)文件,其中$arg包含您传递的值。 As such, it will most definitely accept relative paths. 因此,它绝对会接受相对路径。

>dir /b newdir

>perl -MLWP::Simple=getstore -e"getstore('http://stackoverflow.com/', 'newdir\\so.html')"

>dir /b newdir
so.html

Maybe $filename isn't valid (eg ends with a newline). 也许$filename无效(例如,以换行符结尾)。 Maybe the current directory isn't what you think it is (it can be a directory other than the one containing the script). 也许当前目录不是您想的那样(它可以是包含脚本的目录以外的目录)。 Maybe the user as which the script is executing doesn't have access to the directory. 也许执行脚本的用户无权访问该目录。 You should be able to get more information using: 您应该能够使用以下方法获得更多信息:

use LWP::UserAgent qw( );
my $ua = LWP::UserAgent->new();
my $request = HTTP::Request->new(GET => $url);
my $response = $ua->request($request, "newdir\\$filename");
die($response->status_line)
   if !$response->is_success;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM