Signal Handling and Canceling Operations

The first thing to say is that signal handling in Perl is currently not safe. There is always a small risk of Perl crashing and/or core dumping during or after handling a signal. (The risk was reduced with 5.004_04 but is still present.)

The two most common uses of signals in relation to the DBI are for canceling operations when the user types Ctrl-C (interrupt), and for implementing a timeout using alarm() and $SIG{ALRM}.

To assist in implementing these operations, the DBI provides a cancel method for statement handles. The cancel method should abort the current operation and is designed to be called from a signal handler.

However, it must be stressed that: a) few drivers implement this functionality at the moment (the DBI provides a default method that just returns undef); and b) even if implemented, there is still a possibility that the statement handle, and possibly the parent database handle, will not be usable afterwards.

If cancel returns true, then it has successfully invoked the database engine’s own cancel function. If it returns false, then cancel failed. If it returns undef, then the database engine does not have cancel implemented.

Get Programming the Perl DBI now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.