简体   繁体   English

LWP getstore使用

[英]LWP getstore usage

I'm pretty new to Perl. 我对Perl很陌生。 While I just created a simple scripts to retrieve a file with getstore($url, $file); 虽然我刚刚创建了一个简单的脚本来使用getstore($url, $file);检索getstore($url, $file); But how do I know whether the task is done correctly or the connection interrupted in the middle, or authentication failed, or whatever response. 但是我怎么知道任务是正确完成的还是中间的连接中断,身份验证失败或任何响应。 I searched all the web and I found some, like a response list , and some talking about useragent stuff, which I totally can't understand, especially the operator $ua-> . 我搜索了所有的网络,发现一些内容,例如响应列表 ,以及一些有关useragent的东西,我完全听不懂,尤其是运算符$ua->
What I wish is to an explanation about that operator stuff (I don't even know what -> used for), and the RC code meaning, and finally, how to use it. 我希望对这些运算符(我什至不知道->用于什么),RC代码含义以及最后如何使用它进行解释。
Its a lot of stuff so I appreciate any answer given, even just partially. 它有很多东西,所以我很感谢给出的任何答案,甚至只是部分答案。 And, thanks first for whoever will to help. 而且,首先感谢您能提供帮助的人。 =) =)

The LWP::Simple module is just that: quite simplistic. LWP::Simple模块就是这样:非常简单。 The documentation states that the getstore function returns the HTTP status code which we can save into a variable. 文档指出, getstore函数返回HTTP状态代码,我们可以将其保存到变量中。 There are also the is_success and is_error functions that tell us whether a certain return value is ok or not. 还有is_successis_error函数告诉我们某个返回值是否正确。

my $url      = "http://www.example.com/";
my $filename = "some-file.html";
my $rc = getstore($url, $filename)
if (is_error($rc)) {
  die "getstore of <$url> failed with $rc";
}

Of course, this doesn't catch errors with the file system. 当然,这不会捕获文件系统的错误。

The die throws a fatal exception that terminates the execution of your script and displays itself on the terminal. die会引发致命异常,该异常终止脚本的执行并在终端上显示其自身。 If you don't want to abort execution use warn . 如果您不想中止执行,请使用warn

The LWP::Simple functions provide high-level controls for common tasks. LWP::Simple函数为常见任务提供高级控制。 If you need more control over the requests, you have to manually create an LWP::UserAgent . 如果您需要对请求的更多控制,则必须手动创建LWP::UserAgent An user agent (abbreviated ua ) is a browser-like object that can make requests to servers. 用户代理(缩写为ua )是类似于浏览器的对象,可以向服务器发出请求。 We have very detailed control over these requests, and can even modify the exact header fields. 我们对这些请求有非常详细的控制,甚至可以修改确切的标头字段。

The -> operator is a general dereference operator, which you'll use a lot when you need complex data structures. ->运算符是常规的解除引用运算符,当您需要复杂的数据结构时,将使用很多。 It is also used for method calls in object-oriented programming: 它也用于面向对象编程中的方法调用

$object->method(@args);

would call the method on $object with the @args . 会使用@args$object上调用该method We can also call methods on class names. 我们还可以在类名上调用方法。 To create a new object, usually the new method is used on the class name: 要创建一个新对象,通常在类名上使用new方法:

my $object = The::Class->new();

Methods are just like functions, except that you leave it to the class of the object to figure out which function exactly will be called. 方法就像函数一样,除了将其留给对象的类以弄清楚究竟将调用哪个函数。

The normal workflow with LWP::UserAgent looks like this: 使用LWP::UserAgent的常规工作流如下所示:

use LWP::UserAgent; # load the class

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

We can also provide named arguments to the new method. 我们还可以为new方法提供命名参数。 Because these UA objects are robots, it is considered good manners to tell everybody who sent this Bot. 因为这些UA对象是机器人,所以最好告诉每个发送此Bot的人。 We can do so with the from field: 我们可以使用from字段:

my $ua = LWP::UserAgent->new(
  from => 'ss-tangerine@example.com',
);

We could also change the timeout from the default three minutes. 我们还可以将timeout更改为默认的三分钟。 These options can also be set after we constructed a new $ua , so we can do 我们可以构造新的$ua 之后设置这些选项,这样我们就可以

$ua->timeout(30);  # half a minute

The $ua has methods for all the HTTP requests like get and post . $ua具有用于所有HTTP请求的方法,例如getpost To duplicate the behaviour of getstore , we first have to get the URL we are interested in: 要复制getstore的行为,我们首先必须get我们感兴趣的URL:

my $url = "http://www.example.com/";

my $response = $ua->get($url);

The $response is an object too, and we can ask it whether it is_success : $response也是一个对象,我们可以询问它是否为is_success

$response->is_success or die $response->status_line;

So if execution flows past this statement, everything went fine. 因此,如果执行流过该语句,一切都会顺利进行。 We can now access the content of the request. 现在,我们可以访问请求的内容。 NB: use the decoded_content method, as this manages transfer encodings for us: 注意:请使用decoded_content方法,因为这可以为我们管理传输编码:

my $content = $response->decoded_content;

We can now print that to a file: 现在我们可以将其打印到文件中:

use autodie; # automatic error handling
open my $fh, ">", "some-file.html";
print {$fh} $content;

(when handling binary files on Windows: binmode $fh after opening the file, or use the ">:raw" open mode) (在Windows上处理二进制文件时:打开文件后使用binmode $fh ,或使用">:raw"打开模式)

Done! 做完了!

To learn about LWP::UserAgent , read the documentation . 要了解LWP::UserAgent ,请阅读文档 To learn about objects, read perlootut . 要了解对象,请阅读perlootut You can also visit the perl tag on SO for some book suggestions. 您也可以访问SO上的perl标记以获取一些建议书。

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

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