10.07.2015 Views

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

36CHAPTER 2 ■ POPULAR CGI MODULESTable 2-1. Carp Functions for Outputting Warnings and ErrorsFunctioncarp()cluck()confess()croak()DescriptionSends a warning of errors. It is not fatal.Sends a warning of errors, including a stack backtrace. It is not fatal.Sends a fatal error, including a stack backtrace.Sends a fatal error.Sending Fatal Errors <strong>to</strong> the Browser<strong>With</strong> CGI::Carp, you can configure errors <strong>to</strong> be sent <strong>to</strong> the browser window rather than <strong>to</strong>the log file. This might be useful for debugging and other purposes. However, you shouldbe aware of the potential for this method <strong>to</strong> divulge <strong>to</strong>o much information <strong>to</strong> the end user.In other words, using this method makes debugging and testing easier, but it also may giveout <strong>to</strong>o much information about the script, including its location and other items that youmight not wish disclosed. Therefore, if you’re going <strong>to</strong> use this method, it’s a good idea <strong>to</strong>disable it before going live with the application.To send errors <strong>to</strong> the browser, you need <strong>to</strong> use one of the methods that is not exported bydefault; therefore, you must call it explicitly. Listing 2-1 provides an example of using CGI:Carp<strong>to</strong> send fatal error messages <strong>to</strong> the browser.Listing 2-1. Sending an Error <strong>to</strong> the Browser with CGI::Carp#!/usr/bin/perl -Tuse strict;use CGI qw/:standard/;use CGI::Carp qw/fatalsToBrowser/;print header,start_html("Testing CGI Carp");die ("This is a test die");print end_html;exit;Viewing the code in a browser reveals a page like the one in Figure 2-1.CGI::Carp takes care of most of the background work for you once you call the correctmethod, with this line of code:use CGI::Carp qw(fatalsToBrowser);<strong>From</strong> there, if the script dies, the error will be sent <strong>to</strong> the browser.As you can see in Figure 2-1, the error message also shows contact information. You canconfigure this message by calling the set_message() function. Like the fatalsToBrowser()function, set_message() needs <strong>to</strong> be explicitly imported in<strong>to</strong> your namespace:use CGI::Carp qw(fatalsToBrowser set_message);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!