This might work:
my $timeout = 10; # 10 seconds timeout SOURCE: foreach my $source (@sources) { my dbh = connect_to_db($source); my sth = $dbh->prepare( $your_query ); my $eval_result = eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm( $timeout ); $sth->execute( $your_params ); alarm( 0 ); 1; }; if ( $eval_result ) { last SOURCE; # ALL OK } else { print "Timeout happens!\n"; } }
It might be useful to see also How to timeout if a call to an external program takes too long and Re: Question on "Effective Perl Programming" (;1 > $@).
Note that the above will actually change $@ when it's run, even though it doesn't use it otherwise. If you really want code that doesn't touch $@, using local is the way to go.
In reply to Re: $@ alternative
by kyle
in thread $@ alternative
by Gangabass
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |