How to skip 'die' in perl

送分小仙女□ 提交于 2019-12-11 03:14:05

问题


I am trying to extract data from website using perl API. The process is to use a list of uris as input. Then I extract related information for each uri from website. If the information for one uri is not present it dies. Some thing like the code below

my @tags = $c->posts_for(uri =>"$currentURI");
die "No candidate related articles\n" unless @tags;

Now, I don't want the program to stop if it doesn't get any tags. I want the program to skip that particular uri and go to the next available uri. How can i do it? Thank you for your time and help.

Thank you, Sammed


回答1:


Well, assuming that you're inside a loop processing each of the URIs in turn, you should be able to do something like:

next unless @tags;

For example, the following program only prints lines that are numeric:

while (<STDIN>) {
    next unless /^\d+$/;
    print;
}

The loop processes every input line in turn but, when one is found that doesn't match that regular expression (all numeric), it restarts the loop (for the next input line) without printing.

The same method is used in that first code block above to restart the loop if there are no tags, moving to the next URI.




回答2:


Besides the traditional flow control tools, i.e. next/last in a loop or return in a sub, one can use exceptions in perl:

eval {
    die "Bad bad thing";
};
if ($@) {
    # do something about it
};

Or just use Try::Tiny.

However, from the description of the task it seems next is enough (so I voted for @paxdiablo's answer).




回答3:


The question is rather strange, but as near as I can tell, you are asking how to control the flow of your current loop. Of course, using die will cause your program to exit, so if you do not want that, you should not use die. Seems elementary to me, that's why it is a strange questions.

So, I assume you have a loop such as:

for my $currentURI (@uris) {
    my @tags = $c->posts_for(uri =>"$currentURI");
    die "No candidate related articles\n" unless @tags;
    # do stuff with @tags here....
}

And if @tags is empty, you want to go to the next URI. Well, that's a simple thing to solve. There are many ways.

next unless @tags;
for my $tag (@tags) { ... stuff ... }
if (@tags) { .... }

Next is the simplest one. It skips to the end of the loop block and starts with the next iteration. However, using a for or if block causes the same behaviour, and so are equivalent. For example:

for my $currentURI (@uris) {
    my @tags = $c->posts_for(uri =>"$currentURI");
    for my $tag (@tags) {
        do_something($tag);
    }
}

Or even:

for my $currentURI (@uris) {
    for my $tag ($c->posts_for(uri =>"$currentURI")) {
        do_something($tag);
    }
}

In this last example, we removed @tags all together, because it is not needed. The inner loop will run zero times if there are no "tags".

This is not really complex stuff, and if you feel unsure, I suggest you play around a little with loops and conditionals to learn how they work.



来源:https://stackoverflow.com/questions/6672831/how-to-skip-die-in-perl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!