Sandra Schlichting
Sandra Schlichting

Reputation: 25986

How to not exit on failed download?

How do I download a file with WWW::Mechanize without exiting on failed downloads?

#!/usr/bin/perl

use strict;
use WWW::Mechanize;

my $mech = WWW::Mechanize->new();

$mech->get("http://google.com/test", ':content_file' => "tmp");

print "done";

Upvotes: 1

Views: 305

Answers (3)

Gilles Quénot
Gilles Quénot

Reputation: 185126

You can use autocheck => 0 in the constructor :

#!/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";

Upvotes: 6

matthias krull
matthias krull

Reputation: 4429

use 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";

Leave out the catch block if you just want to silence errors.

Upvotes: 4

snoofkin
snoofkin

Reputation: 8895

does it dies on failed downloads? if so, try wrapping that call to 'get' with eval..

Upvotes: 1

Related Questions