Your last code example is fine. It doesn't use global variables, since $q is declared as lexical by my. In effect, your carp_error subroutine is acting as a closure. In fact, you could go all the way and create your error-handling routine as an anonymous subroutine. That way it won't stay around cluttering your symbol tables.

You also don't need to create your CGI object in a BEGIN block, as long as you make sure your error handler has some CGI object available. In my code example below, the line marked with "!!!" does that. Note the brackets ({}) on that line -- they make the "backup" CGI object completely independent from the actual HTTP request. That way, if your script dies because of a malformed request, it hopefully will still be able to generate its error message.

use CGI::Carp qw( fatalsToBrowser ); use CGI qw( -headers_once ); my $q = new CGI; BEGIN { CGI::Carp::set_message sub { my $error = shift; $q ||= new CGI {}; # !!! print $q->header(), $q->start_html( "Error" ), $q->h1( "Error" ), $q->p( "Sorry, the following error occurred:" ), $q->pre( $q->i( $error ) ), $q->end_html(); }; }

Also worth noting in the use of $q->pre() for the actual error. Perl error messages are preformatted with indents and line breaks, and look rather messy if smashed up in a single HTML paragraph.

Now, once you've gone to all that trouble and finally test your code, you will notice that you're still sending double HTTP headers. Why? Well, there's this little detail in the CGI::Carp documentation:

CGI::Carp arranges to send a minimal HTTP header to the browser so that even errors that occur in the early compile phase will be seen.

Damn. Time to submit a patch, I guess. Meanwhile, I suppose you can work around the issue by inserting the following lines at the end of the BEGIN block:

## REALLY UGLY KLUGE FOLLOWS, USE AT YOUR OWN RISK: my $fatalsToBrowser = \&CGI::Carp::fatalsToBrowser; no warnings 'redefine'; *CGI::Carp::fatalsToBrowser = sub { local $ENV{GATEWAY_INTERFACE} = "CGI-PerlEx-FAKE"; &$fatalsToBrowser; }

In reply to Re: Proper use of CGI::Carp together with CGI.pm by iltzu
in thread Proper use of CGI::Carp together with CGI.pm by davido

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.