in reply to $@ alternative
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: $@ alternative
by ikegami (Patriarch) on Sep 10, 2007 at 12:44 UTC |