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

One of the things I like about DBI is that I can automatically trap errors.
my $dbh = DBI->connect("dbi:ODBC:$DSN", $USER, $PASSWORD, {RaiseError +=> 1});
That RaiseError statement allows subsequent calls to the database to automatically die with a message on an error. If you don't add that and fail to check for success or failure, your program can chug silently along, functioning incorrectly, without you having a clue until your boss yells at you.

My dilemma: the company that I work for has a lot of legacy code in modules that opens files, reads them, establishes database connections, etc., ALL WITHOUT CHECKING FOR SUCCESS OR FAILURE!!! I am going through and fixing these things when I find them, but I am wondering if their is a way to automatically kill your Perl script if something like open FILE, $somefile fails? I'm not talking about adding or die on all of these. I'm doing that now. I'm wanting something that will autokill the script if $! is set. That would just be a stopgap workaround, but I need to have something in place until I can plug all of the holes. (Yes, I know that most database connection failures don't set $!. Checking for a change in $! would catch the bulk of the problems).

Until I get all of these things fixed, this is going to be a nightmare. As is stands now, I just spent a couple of hours finding a bug in a huge script that I would have found immediately had they just put an "or die" after a stinkin' open statement!

Cheers,
Ovid

Replies are listed 'Best First'.
Re: Error trapping
by merlyn (Sage) on Sep 05, 2000 at 20:44 UTC
    Check into the Fatal module, included with the distribution, which overloads most of the normal built ins so that they throw trappable errors instead of simply returning false.

    -- Randal L. Schwartz, Perl hacker

RE: Error trapping
by Adam (Vicar) on Sep 05, 2000 at 20:51 UTC
    I wonder if you could tie something to $!. Although I suspect the magic nature of $! would get in the way, since I doubt that the tied function would be called when $! got set. But it was a thought.