vit has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I know how to handle "local" errors using eval or $SIG{__DIE__}. But I want to handle errors occuring inside a package which I do not want to touch.
In my particular case it is
Can't use an undefined value as an ARRAY reference at c:/ActivePerl/si +te/lib/CAM/PDF/PageText.pm line 57, <STDIN> line 1
I do not know why it is happening in PageText.pm and can afford to skip the PDF file where it is happening and proceed with the next file without aborting the program.

Replies are listed 'Best First'.
Re: Handling errors occuring inside complex modules
by ikegami (Patriarch) on Jan 19, 2010 at 00:27 UTC

    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.

      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.

Re: Handling errors occuring inside complex modules
by SpiceMan (Sexton) on Jan 19, 2010 at 00:18 UTC

    The poor man's hack is using eval to do the error handling.

    eval { die "Goodbye cruel world" }; warn "eval died with $@" if $@;

    eval{}; if($@) {} probably is the most common practice, but is ugly and error prone.

    Other alternatives include error-handling modules such as TryCatch or Error (be aware that Error and Moose conflict if you use Moose.)

    Try them (and other modules too!) and use which one feels more comfortable.

    Update: I had completely missed you mentioned eval...

      Other alternatives include error-handling modules such as TryCatch or Error (be aware that Error and Moose conflict if you use Moose.)

      Actually Error is no longer recommended, it even says so in the POD

      Using the "Error" module is no longer recommended due to the black-magical nature of its syntactic sugar, which often tends to break. Its maintainers have stopped actively writing code that uses it, and discourage people from doing so. See the "SEE ALSO" section below for better recommendations.
      I personally recommend Try::Tiny instead (and it plays well with Moose)

      -stvn