Home > Articles > Web Development > Perl

  • Print
  • + Share This

Error Handling

Because something can always go wrong, it is important to check for potential errors and declare them in a format that the Web server and browser understand.

One way to catch error messages is by redirecting them to the Web browser as they appear, perhaps even setting them apart in red text. This method is commonly seen in Active Server Pages (ASP)-style applications when a particular snippet of code fails to run or returns an error. Unfortunately, the error messages produced by Perl are of more interest to the programmer of the application than to the user. Thus, you should treat common or foreseeable errors with a little more charm.

Perl errors are produced in plain text and don't necessarily require the program to halt, so common errors can be found and restated in a way that is more understandable to the program's user. For example, a query that is rejected by the specified data source might give a cryptic error message and a useless line number if allowed to halt the program on its own. Catching the error and presenting it in plain English can greatly improve the usability of a program.

It's also important to give the user a chance to correct any typos or other errors without having to type the entire query again. This is the situation in which the form creation qualities of CGI.pm come in handy. If the user has filled in the form fields and the values are present as variables—as the data source and query variables are likely to be—the CGI query object inserts those values into the form's input fields as it creates them. The form creation methods don't have to be modified for this case, which reduces both duplication of effort and the complexity of the program.

This example is very basic, but the general principles involved can be used to enable any number of Web-based database interfaces. In fact, this example is used as the basis for many of the advanced topics in my book. Because Perl enables code to be extended easily, it's possible to implement design templates or embed the code in HTML pages. It's also possible to improve the performance of programs like these by adding persistence or using the advanced capabilities of DBI. With Perl as the springboard, your options are unlimited.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.