(You are Anonymous)

Why It's Better to Refrain from Using CGI::Carp

The 'error_mode' method provided by CGI::Application since version 3.30 allows a developer to specify the name of a runmode that CGI::Application will use to handle errors that arise during execution of any other runmode. The value of Perl's $@ variable is sent by CGI::Application as a parameter to the runmode designated as the error mode. The return value is displayed in the browser.

For example:

package Event;

. . . 

sub setup {
   . . .
   $self->error_mode( 'my_error_mode' );
   . . .
}

sub my_error_mode {
   my ( $self, $err ) = @_;
   my $display = "Error encountered: '$err'; stopped";
   return $display;
}

sub process_form {
   . . .
   die "Does not look like an email address"  # This is line 47
     unless ( $q->param('email') =~ /@/ );
   . . .
}

If a CGI::Application script using the above Event.pm module were invoked by a form into which the user had entered "foo" as his email address, this message would be sent to STDERR:

Error encountered: 'Does not look like an email address'; stopped at /www/cgi-bin/cms/lib/Event.pm line 47.

CGI::Application by default very nicely reports the line number at which a runtime error has occurred.

Unfortunately CGI::Application's error_mode method can't be used to specify a runmode to use in handling a compile-time error. It handles only errors that arise inside a runmode (i.e., runtime errors).

Compile-time errors cause the web server to generate an unhelpful "500 Server Error" page that contains none of the information sent to STDERR, so I often use CPAN's CGI::Carp module during script development ("use CGI::Carp qw(fatalsTo Browser);").

That module works in a CGI::Application script. I discovered, though, that using CGI::Carp causes CGI::Application to report an unhelpful line number in another module (Carp.pm) rather than the line number in the script where the runtime error occurred. Using CGI::Carp in the sample code above would cause the error to look like this:

Error encountered: 'Does not look like an email address'; stopped at /usr/local/lib/perl5/5.8.6/CGI/Carp.pm line 314.

Losing the line number for runtime errors seems to me to wipe out the benefit of having CGI::Carp report compile-time errors in a web browser, so I no longer use it with CGI::Application scripts.

(Compile-time errors can be detected using the command line, of course, as in "perl -c my_app.cgi".)

Dave Baker