好的旧LWP允许您将结果作为流处理.
例如,这里是对yourFunc的回调,读取/传递byte_count字节到每次调用yourFunc(如果不关心每次调用的数据有多大,你可以删除该参数,并且只想尽快处理流):
use LWP;
...
$browser = LWP::UserAgent->new();
$response = $browser->get($url,
':content_cb' => \&yourFunc,
':read_size_hint' => byte_count,);
...
sub yourFunc {
my($data, $response) = @_;
# do your magic with $data
# $respose will be a response object created once/if get() returns
}
Run Code Online (Sandbox Code Playgroud)
HTTP :: Lite的request
方法允许您指定回调.
该
$data_callback
参数(如果使用)是一种在接收数据时过滤数据或处理大型传输的方法.它必须是函数引用,并且将被传递:对进行回调的http请求的实例的引用,对将要添加到正文的当前数据块的引用,以及$cbargs
参数(可以是任何内容) .它必须返回对要添加到文档正文的数据的引用,或者undef.
然而,看着源,有似乎是在一个错误使用起来似乎更安全sub request
的,因为它似乎忽略过去了回调.set_callback
:
#!/usr/bin/perl
use strict;
use warnings;
use HTTP::Lite;
my $http = HTTP::Lite->new;
$http->set_callback(\&process_http_stream);
$http->http11_mode(1);
$http->request('http://www.example.com/');
sub process_http_stream {
my ($self, $phase, $dataref, $cbargs) = @_;
warn $phase, "\n";
return;
}
Run Code Online (Sandbox Code Playgroud)
输出:
C:\Temp> ht connect content-length done-headers content content-done data done
看起来传递给request
方法的回调被区别对待:
#!/usr/bin/perl
use strict;
use warnings;
use HTTP::Lite;
my $http = HTTP::Lite->new;
$http->http11_mode(1);
my $count = 0;
$http->request('http://www.example.com/',
\&process_http_stream,
\$count,
);
sub process_http_stream {
my ($self, $data, $times) = @_;
++$$times;
print "$$times====\n$$data\n===\n";
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4089 次 |
最近记录: |