Home arrow Perl Programming arrow Page 8 - Carping About DBI

Croak! - Perl

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.

TABLE OF CONTENTS:
  1. Carping About DBI
  2. Dissecting The DBI
  3. Animal Antics
  4. Do()ing More
  5. When Things Go Wrong
  6. Speed Demon
  7. Dummy Data
  8. Croak!
  9. Whining Some More
  10. Final Thoughts
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 1
May 02, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
The Carp module comes with four functions which are quite symmetrical in form and function. Two functions, carp() and cluck(), only print out warnings, while two other functions, croak() and confess(), terminate the script and also print out a sequence of error messages. Both cluck() and confess() can also print out the stack backtrace, making it easier to pinpoint the problem.

The Carp module can be used wherever descriptive error messages warning you about potential problems are required. They're not going to make your program run any better, but they will simplify and streamline the process of error detection and recovery.

It should be noted that although, they provide much-needed information, all the Carp functions internally call warn() or die(). However, since they're very lightweight and offer numerous advantages, I'd still recommend that you use them in preference to both die() and warn().

I've rewritten the example above to demonstrate the utility of Carp.

#!/usr/bin/perl
use Carp;
sub readFile
{
my $filename = shift(@_);
open(FILE, $filename) or croak("Cannot locate file!");
print <FILE>;
close FILE;
}
readFile("dummy.txt");


And here's the output.


Cannot locate file! at ./carpdemo.pl line 8
main::readFile('dummy.txt') called at ./carpdemo.pl line 13



This article copyright Melonfire 2001. All rights reserved.

 
 
>>> More Perl Programming Articles          >>> More By Vikram Vaswani, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: