in reply to Handling errors occuring inside complex modules

I don't follow. You say you know how to use eval, but it seems to me it's the obvious answer:

for my $pdf (@pdfs) { eval { process $pdf with module }; }

The eval will catch the error and allow you to process the next PDF file.

Replies are listed 'Best First'.
Re^2: Handling errors occuring inside complex modules
by vit (Friar) on Jan 19, 2010 at 04:54 UTC
    I tried eval but it did not help.
    However, I might did something wrong. Let me know, please, as { process $pdf with module } can I do something like
    eval { $value = SomeSub() } .............. sub SomeSub { use Module; ....... return Module::xxx (); }
    A stupid question. Why eval call uses {}?

      Are you trying to catch errors loading the module? If so, that won't work. use Module occurs at compile-time. Change it to require Module;. But trying to resume is probably not going to work since you have a half-loaded module. On the plus side, the error you gave is unlikely to occur from loading a module.

      Otherwise, yes, it will catch exceptions thrown by SomeSub and subs called by SomeSub (and subs called by subs called by ...).

      Exceptions unwind out of every sub until an eval catches it or until Perl itself is exited.

      sub SomeOtherSub { print "c\n"; undef->foo(); print "d\n"; } sub SomeSub { print "b\n"; SomeOtherSub(); print "e\n"; } print "a\n"; eval { SomeSub(); 1 } or print("Caught an error!\n"); print "f\n";
      a b c Caught an error! f

      Honestly, you've been very vague in describing the problem you want to solve. It's very hard to help you.

      Why eval call uses {}?

      Arguments are normally evaluated before the function to which they are passed. In eval's case, the argument is a block of code which will be evaluated by eval itself. do, map and grep are other functions that takes a code block for argument. They are similar to for and while, but these two can't be used in expressions.

        Thanks a lot! eval{} is working for me now.
        One question. Is it possible that it negatively impacts performance?