开发者

Error handling in perl api

I am trying to extract data from website using Perl API. I am using a list of URIs to get the data from the website.

Initially the problem was that if there was no data available for the URI it would die and I wanted it to skip that particular URI and go to the next available URI. I used next unless ....; to come over this problem.

Now the problem is I am trying to extract specifi开发者_如何学Goc data from the web by calling a specific method (called as identifiers()) from the API. Now the data is available for the URI but the specific data (the identifiers), what I am looking for, is not available and it dies.

I tried to use eval{} like this

eval {
    for $bar ($foo->identifiers()){
        #do something
    };
}

When I use eval{} I think it skips the error and moves ahead but I am not sure. Because the error it gives is "Invalid content type in response:text/plain".

Whereas I checked the URI manually, though it doesn't have the identifiers it has rest of the data. I want this to skip and move to next URI. How can I do that?

I got a reply from one of the experts that: When Perl hits an error, like most languages, it runs out through the calling contexts in order until it finds a place where it can handle the error. Perl's most basic error handling is eval{} (but I'd use Try::Tiny if you can, as it is then clearer that you're doing error handling instead of some of the other strange things eval can do). Anyway, when Perl hits eval{}, the whole of eval{} exits, and $& is set to the error. So, having the eval{} outside the loop means errors will leave the loop. If you put the eval{} inside the loop, when an error occurs, eval{} will exit, but you will carry on to the next iteration. It's that simple.

But really the error I get is invalid content type in response: text/html at mycode line 41 and the line 41 in my code (my program) is really to the data from web. I know there is no error in the code because it works fine for previous uris. Now, how do I fix this error and move ahead to next uri? my program stucks at this error.


I'm now assuming your code looks something like this:

while (<IN0>) {
    my $currentURI = $_; 
    chomp($currentURI); 
    my @tags = $c->posts_for(uri =>"$currentURI");
    ... do something ...
}

To handle errors, safely, use something like this (assuming eval()):

while (<IN0>) {
    my $currentURI = $_; 
    chomp($currentURI); 
    my @tags = eval {        # Use eval to make this safe, $@ will contain any error
        $c->posts_for(uri =>"$currentURI");
    }
    my $error = $@;
    if (defined($error)) {
        warn($error);        # If eval failed, warn and skip to next line
        next;
    }
    if (! @tags) {
        next;                # Assuming no tags means go to next line in input
    }
    ... do something ...
}

All eval really does here is guarantee that Perl errors which can be handled leave the eval block in the normal way, although if that happens, @tags will be empty, and $@ will contain the error. The remaining logic warns (and skips the input line) if $@ was set. You likely want to skip the line anyway if there are no tags.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜