Search code examples
perlgetwww-mechanize

Ignoring GET error of an unexisting webpage


I use WWW::Mechanize to fetch and process web pages. I have a piece of code, which looping through a list of web pages. It looks approximately like this:

while (<$readFileHandle>) {
    $mech->get("$url");
}

Now the problem occurs when one of the web pages in the list does not exist for some reason(which is ok). The issue is that in this case - the program returns an error and exits. The error looks like that:

Error GETing <url> Not Found at <PATH/file.pl> line ...

How can I ignore such type of error? I want the program just keep running.


Solution

  • You need to use eval {}; for this:

    while ( my $url = readline($readFileHandle) ) {
        chomp $url;
        eval {
            $mech->get($url);
        };
        if ($@) {
          #error processing code
        }
    }