set_err()
$handle->set_err($err, $errstr[, $state[, $method[, $return_value]]])
This function sets the values for err,
errstr, and state for the
handle. The method (e.g.,
RaiseError) can be changed as well. It returns
undef unless a different return value is given as
the fifth argument to this method. You can use this manually to return
an error message to a user. Here is an example:
...
my $book_id = shift;
my $books = &get_data_ref($book_id)
or print "Error: " . DBI->err . DBI->errstr;
...
sub get_data_ref {
my $book_id = shift;
if($book_id =~ m/\D/g) {
return $dbh->DBI::set_err(500, "\nYou entered '$book_id'.\nBad Book
ID!");
last;
}
...
}Notice in the subroutine that if it is given a book identifier
that contains any nonnumeric characters, it does not proceed and
instead returns the error as set by set_err. The
line of code at the top of the excerpt that calls the subroutine will
display the results if true, or display the error number and string.
Here are the results of the program when a user enters a book ID that
contains a letter:
Error: 500 You entered '100g'? Bad Book ID!