Run the DBI code in a eval block and check $@ after the block.
See the Fine manual for an example, and look at perldoc -f eval
and perldoc -f die
Example:
eval {
#DBI code here
};
if ( $@ ) {
#we died in eval block
#$@ contains the error string
trap_die( $@ );
}
-----Original Message-----
From: dba dba [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, September 25, 2001 5:04 PM
To: [EMAIL PROTECTED]
Subject: Help:how to call email function when sees error?
Hi, Folks
I have a perl scripts like this:
------------------------------------------------------
sub trap_die
{
my @param = @_;
$message = $param[0];
$CommandString = "/bin/mailx -s \"$message\" xxxx\@yyyy.com ";
system ( $CommandString );
}
use strict;
use DBI;
my $dbh = DBI->connect ( 'dbi:Oracle:test',
'system',
'manager',
{
PrintError => 0,
RaiseError => 1,
AutoCommit => 1
}
) || die "Database Connection not made
$DBI::errstr" ;
my $sql = qq{ select * from global_name}
my $sth = $dbh ->prepare($sql);
$sth->execute();
while (my @rows = $sth->fetchrow_array) {
print "@rows[0]\n";
}
$sth->finish();
$dbh->disconnect();
-------------------------------------------------------
For any reason there is an error, perl will exit. Everyday I have to
read output files to see whether there is an error, how can I make a
perl call my sub function TRAP_DIE so that when error, I will get an
email?
I tried to put the line
if ($dbh->error) {trap_die("error");}
but it will not work because the perl will exit as soon as it raise
error.
Thanks for your help.
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp