在EVAL跳过错误{}声明 [英] Skipping error in eval{} statement

查看:180
本文介绍了在EVAL跳过错误{}声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图提取使用Perl API的网站的数据。我使用URI列表从网站上获取数据。

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.

最初的问题是,如果没有数据可用于URI它会死,我想它跳过特定URI并转到下一个可用的URI。我用接下来,除非......。过来这个问题。

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.

现在的问题是我想通过调用一个特定的方法(称为标识符())从API提取网页的具体数据。现在的数据是可用的URI但具体数据(标识),我所寻找的,不可用,死了。

Now the problem is I am trying to extract specific 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.

我试图用的eval {} 像这样

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

当我使用的eval {} 我觉得它跳过错误并向前推进,但我不知道。因为它给人的误差响应无效的内容类型:纯文本/

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.

虽然我人工检查该UR​​I,尽管它不具有标识符它具有数据的其余部分。我想这和跳跃移动到下一个URI。我该怎么做?

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?

推荐答案

OK,我想我明白你的问题,却多了几分code会有所帮助,因为会指明哪些Perl的API - 不,它似乎无关紧要的答案,但它是你的问题的重要组成部分。话虽如此,这个问题似乎很简单。

OK, I think I understand your question, but a little more code would have helped, as would specifying which Perl API -- not that it seems to matter to the answer, but it is a big part of your question. Having said that, the problem seems very simple.

在Perl中命中一个错误,最喜欢的语言,它运行通过,以便调用上下文,直到找到一个地方,它可以处理的错误。 Perl的最基本的错误处理的eval {} (但我会使用尝试::微小如果可以的话,作为它是那么明显的是你在做错误处理,而不是其他一些奇怪的事情的eval可以做)。

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).

无论如何,当Perl的命中的eval {} ,整个中的eval {} 退出,和 $&安培; 设置为错误。因此,具有的eval {} 外循环是指错误将退出循环。如果你把的eval {} 的循环,当错误发生时,的eval {} 将退出,但你会继续到下一个迭代。就这么简单。

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.

我还发现,也许你不使用使用严格的迹象; 使用警告; 。请这样做,因为他们帮助你发现许多错误更快。

I also detect signs that maybe you're not using use strict; and use warnings;. Please do, as they help you find many bugs quicker.

这篇关于在EVAL跳过错误{}声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆