RedGrinGo has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I made a simple script which should download some file to my PC, the program make a request with LWP and from the HTML I get the links to the files I need into an array. Now I use WWW::Mechanize to get the files (I know that there are some other options but I can use only this one). when there is a link that appears to be wrong the program exit with an error: "Error GETing some_url Internal Server Error at myscript.pl line 171" Now what I need is, even if there is an error I want the script to move to the next URL on the Array without exiting. Any suggestions ???

sub getFile{ my @files = @_; my $mech = WWW::Mechanize->new; my $fileIndex = 1; foreach my $file (@files){ $mech->get( $file, ':content_file' => "myFile$fileIndex.txt" ) +; $fileIndex++; } }

Replies are listed 'Best First'.
Re: WWW::Mechanize Error "GETing" stop the program,
by Corion (Patriarch) on Apr 14, 2010 at 13:48 UTC

    See WWW::Mechanize on autocheck. This makes HTTP errors appear as Perl errors, which usually helps you if you are not checking for errors yourself. If you pass in autocheck => 0, then you will need to start checking for errors yourself:

    if ($mech->get( $file, ':content_file' => "myFile$fileIndex.txt" ) +) { ... } else { # signal error to user };

      Even when the "autocheck" is set to default which is 1 the program still exit

        I didn't say anything about setting autocheck to 1. I specifically mentioned setting autocheck => 0, which is documented in WWW::Mechanize.

Re: WWW::Mechanize Error "GETing" stop the program,
by Anonymous Monk on Apr 14, 2010 at 13:43 UTC
    foreach my $file( @files ){ eval { $mech->get... 1; } or warn $@' $fileIndex++; }

      Please read the WWW::Mechanize documentation. There is no need for the sledgehammer approach to catching exceptions from ->get, as WWW::Mechanize will happily cooperate if you allow it to.

        No thanks, I like the life or death approach

      Hi Thanks

      what is the number 1 reffer in the script?

        True value. I never remember if get return true on success so I always type 1, because I know that it will die on failure , which always returns false (and then the or warn part is executed).