Mixed error checking
You can mix error checking styles within a single program, since automatic error checking can be easily enabled and disabled on a per-handle basis. There are plenty of occasions where mixed error checking is useful. For example, you might have a program that runs continuously, such as one that polls a database for recently added stock market quotes every couple of minutes.
Disaster occurs! The database crashes! The ideal situation here is that the next time the program tries connecting to the database and fails, it’ll wait a few minutes before retrying rather than aborting the program altogether. Once we’ve connected to the database, the error checking should now simply warn when a statement fails and not die.
This mixed style of error checking can be broken down into two areas:
manual error checking for the
DBI->connect( ) call, and
automatic error checking via PrintError for all
other statements. This is illustrated in the following example
program:
#!/usr/bin/perl -w # # ch04/error/mixed1: Example showing mixed error checking modes. use DBI; # Load the DBI module ### Attributes to pass to DBI->connect( ) to disable automatic ### error checking my %attr = ( PrintError => 0, RaiseError => 0, ); ### The program runs forever and ever and ever and ever ... while ( 1 ) { my $dbh; ### Attempt to connect to the database. If the connection ### fails, sleep and retry until it succeeds ... until ( $dbh = DBI->connect( "dbi:Oracle:archaeo", "username", "password" , ...