WWW::Mechanize
失敗したダウンロードを終了せずにファイルをダウンロードするにはどうすればよいですか?
#!/usr/bin/perl
use strict;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
$mech->get("http://google.com/test", ':content_file' => "tmp");
print "done";
autocheck => 0
コンストラクターで使用できます:
#!/usr/bin/perl
use strict;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new(
# perldoc WWW::Mechanize | less +/autocheck
autocheck => 0
);
$mech->get("http://google.com/test", ':content_file' => "tmp");
# Now, need to check errors manually :
# test on the HTTP return code see :
# http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
my $retcode = $mech->status();
if ($retcode >= 400) {
warn("error getting url, HTTP code: [$retcode]\n");
}
print "done\n";
Try::Tinyを使用してください
#!/usr/bin/perl
use strict;
use WWW::Mechanize;
use Try::Tiny;
my $mech = WWW::Mechanize->new();
try {
$mech->get("http://google.com/test", ':content_file' => "tmp");
}
catch {
print "failed: $_";
}; # <-- do not forget the semicolon
print "done";
catch
エラーを消音したいだけの場合は、ブロックを省略してください。
ダウンロードに失敗すると死にますか?もしそうなら、'get'への呼び出しをeval
..でラップしてみてください。