One of the nice things about Perl is the huge amount of free codeout there. Available in the form of modules, this code can simplify manycommon tasks while simultaneously offering a powerful toolkit for theexperienced developer. In this article, learn about two of the most popularPerl modules: DBI, used for database connectivity, and Carp, used tosimplify error handling.
No matter how good you are, you're bound to screw up sometime. And when it happens, you'll be glad that I told you about $DBI::errstr, used to display error messages when things go wrong.
# connect
my $dbh = DBI->connect("DBI:mysql:database=somedb;host=localhost", "me",
"me545658") || die "Can't connect: $DBI::errstr";
Additionally, you can also check the handle for errors during a query - as the following example demonstrates.
while(my $ref = $sth->fetchrow_hashref())
{
print "Name: $ref->{'Name'} Species: $ref->{'Species'} Age: $ref->{'Age'}
\n";
if($sth->err)
{
die "There was an error: $sth->errstr";
}
This article copyright Melonfire 2001. All rights reserved.