my $failed;
eval {
local $SIG{ALRM} = sub { die "TIMEOUT\n" }; # N.B. \n required
eval {
alarm($seconds);
... code to execute with timeout here (which may die) ...
1;
} or $failed = 1;
# outer eval catches alarm that might fire JUST before this alarm(
+0)
alarm(0); # cancel alarm (if code ran fast)
die "$@" if $failed;
1;
} or $failed = 1;
if ( $failed ) {
if ( defined $@ and $@ eq "TIMEOUT\n" ) { ... }
else { ... } # some other error
}
I am not able to replicate your code so I can identify the problem but the code bellow works for me:
my $failed;
my $seconds = 12;
eval{
Config::Simple->import_from("".$path."", \%config)
or die Config::Simple->error();
my $dbh = DBI->connect("dbi:mysql::".$config{'MySQL.host'}.":".$co
+nfig{'MySQL.port'}."",
"".$config{'MySQL.user'}."",
"".$config{'MySQL.pass'}."",
{ 'PrintError' => 1, 'RaiseError' => 1 , 'AutoInactiveD
+estroy' => 1 }
) or die "Could not connect to ". $config{'MySQL.host'} .": ". $DB
+I::errstr ."\n";
local $SIG{ALRM} = sub { die "db_timeout" };
eval {
alarm($seconds);
$dbh->do("USE Thanos1983");
# or die "Error: " .dbh->errstr. "\n";
1;
} or $failed = 1;
# outer eval catches alarm that might fire JUST before this alarm(
+0)
alarm(0); # cancel alarm (if code ran fast)
die "$@" if $failed;
1;
} or $failed = 1;
if ( $failed ) {
if ( defined $@ and $@ eq "TIMEOUT\n" ) { ... }
else { print "Error\n" } # some other error
}
__END__
$ perl test.pl
DBD::mysql::db do failed: Unknown database 'Thanos1983' at test.pl lin
+e 30.
Error
Also usually when I use prepare() I use it like this:
my $sth = $dbh->prepare("SELECT `ID`, `Y-Values` FROM `".$config{'MySQ
+L.table'}."` WHERE 1");
if (!$sth->execute()) {
die "Error: ". $sth->errstr ."\n";
}
Update: For login purposes I prefer to use Log::Log4perl.
Hope this helps, BR.
Seeking for Perl wisdom...on the process of learning...not there...yet!
|