Search iEntry News

Improved Exception Handling In Perl 5

By Taylor Gillespie
Expert Author
Article Date: 2010-11-16

Exception handling, or simply error handling, in Perl 5 has always been a matter of using eval on statements and then using conditional statements to check special variables for any possible error codes and responding appropriately. Many pitfalls exist in in this method including the scope of the special variables or their state, and the ancillary code can make readability and maintenance more troublesome.

Most other languages have certain exception handling constructs, namely try-catch-finally blocks, and indeed, the Perl 6 implementation includes try-catch in the core language. Perl 5, too, with certain modules can mimic true exception handling.

Perhaps the most well known Perl 5 module for implementing try-catch-finally blocks is TryCatch. The module brings "first class try catch semantics for Perl, without source filters," and is a more complete exception handling in the style of other object-oriented languages. For Perl purists, this modules might be a bit too much code for more simple error handling situations. Developers that need a more stripped-down try-catch construct that imposes very little in terms of forcing a particular coding style or necessary ancillary code, then the Try::Tiny module is very appealing. Try::Tiny removes many of the problems associated with using eval and if-then to check for errors with lots of overhead. Because it is a light-weight replacement for using eval, Try::Tiny has some usage caveats, but improves the error handling experience
.
Proper error handling in Perl 5 requires managing special variables and having a knowledge of their state, and state change. Perl 6 improves the error handling by introducing try-catch blocks typical of other high-level languages. Modules, such as TryCatch and Try::Tiny, help to clean-up the error handling constructs of Perl 5 without the use of source filters. Try::Tiny is a great choice because it is light-weight and adds immediate value with enchanced error handling.

About the Author:
Taylor is a Staff Writer for WebProNews




Newsletter Archive | Article Archive | Submit Article | Advertising Information | About Us | Contact

PerlProNews is an iEntry, Inc. ® publication - All Rights Reserved Privacy Policy and Legal