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

How can I capture an Error from a perl module in a perl script?

I have a perl script using a module, and when the module encounters an error it dies and prints an error to the screen. I need to capture this output it, manipulate it, and then print it out.

Any ideas?

Replies are listed 'Best First'.
Re: Capturing STDERR from a module
by Ovid (Cardinal) on May 16, 2003 at 19:32 UTC

    Use eval.

    use SomeModule; my $foo = SomeModule->new; eval { $foo->die_die_die }; if ($@) { print "There was a problem, but we didn't die: $@"; } else { print "Not a problem."; }

    As the code above suggests, eval will (usually) prevent the module from dieing and $@ will contain the error.

    Cheers,
    Ovid

    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)

      Great, Thanks :)