0

I have a Perl script and I am trying to make it print out the value for $article when it errors. The script looks like:

eval{
    for my $article($output =~ m/<value lang_id="">(.*?)<\/value>/g)
    { 
        $article =~ s/ /+/g;
        $agent->get("someurl");

        $agent->follow_link(url_regex => qr/(?i:pdf)/ );

        my $pdf_data = $agent->content;
        open my $ofh, '>:raw', "$article.pdf"
        or die "Could not write: $!";
        print {$ofh} $pdf_data;
        close $ofh;
        sleep 10;
    }
};
if($@){
    print "error: ...: $@\n";
}

So if there is no .pdf file the code sends an error which is what I want. But what I need to know is it somehow possible to get the name of the $article that caused the error? I was trying to use some kind of global variable with no luck.

4 Answers 4

4

Why don't you put the eval inside the for loop? Something like this:

for my $article($output =~ m/<value lang_id="">(.*?)<\/value>/g)
{ 
   $article =~ s/ /+/g;
   eval{
      # ...
   }
   if ($@) {
      print STDERR "Error handling article: ", $article, " ", $!, "\n";
   }
}
Sign up to request clarification or add additional context in comments.

5 Comments

For some reason when I do that it prints every pdf not just the ones equal to $article from the for loop
ITYM print STDERR "Error handling article $article: $@\n"; (not $!). And you can use warn instead of print STDERR. But in general, this should work; if it doesn't work for @chrstahl89, the problem is probably somewhere inside the eval.
@chrstahl89: Can you post the actual code that "prints every pdf"?
Posted it to my original question
It was my code that was wrong in another part, this was the correct solution I just did not know it at the time. Thanks!
1

If that's your only problem, just declare my $article; before the eval, and remove the my from the for loop. But from your reply to Cornel Ghiban, I suspect it isn't.

Comments

0

Include the file name in the die>/ string:

open my $ofh, '>:raw', "$article.pdf" or die "Could not write '$article': $!";

I assume that you want to write and not read. Unless you have a permission issue or a full file system, a write is likely to succeed and you will never see an error.

3 Comments

This will not work because the error is from getting the link, it never goes to write.
That wasn't clear to me in your original question. That said, follow-link returns undef when the page has no links or the link can;t be found. You could throw an exception for that.
I think that will be the answer, just have to figure out how to throw an error for that. Well catch the error...it already throws it.
0

Your script does not need to die, you can just set a flag or save message to the log or store error for late handling.

my @errors=();
................
open my $ofh, '>:raw', "$article.pdf" or do { push @errors,"$article: $!" };
if(-e $ofh) {
    # work with the file
}
................
if(@errors) {
    # do something
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.