Or, why is the script sending two headers?
# minimum code to illustrate point # based on example in 'CGI programming with Perl' use CGI; use CGI::Carp qw( fatalsToBrowser set_message ); $CGI::HEADERS_ONCE = 1; BEGIN { sub carp_error { my $message = shift; error( $message ) } CGI::Carp::set_message( \&carp_error ) } my $q = new CGI; # time passes # the next line wouldn't cause a duplicate header # die; print $q->header( 'text/html' ); # this next line would # die; # send the broswer a message print "test"; exit; sub error{ # die nicely exit }

The reason is clear and easily available in the docs, perlmonks and many books that I have. It still took me a long time to work out! I hope this may help others.

1. HEADERS_ONCE applies to each CGI query object.
2. set_message always sends a header.

So depending on when the error occurs the browser may or may not display an unwanted content header. That is, if the error occurs before or after this line:

print $q->header( 'text/html' );

Conclusion?
The excellent "CGI programming with Perl" says this is "one of the biggest challenges for catching errors". Sadly, I had the same problem with the solution it offers. I'm not up to small challenges never mind big ones! So, I only use CGI::Carp during debugging.

Out of intrest, I identified the error I was chasing by studying the access log. I was relying on $ENV{HTTP_REFERER} to identify which page had called the script.
I've seen this suggested on perlmonks in answer to a question about maintaining state. But again, the docs always say 'not all browsers support this' (and did I take any notice!).
The script was throwing 500 errors because HTTP_REFERER was '' (blank).
I have a robot.txt telling robots to ignore my script folder.
While I ponder what to do I am trapping the condition and saying 'service temporarily unavailable'!
I'm considering using javascript to send the file name appended as a query. It's a simple 'printer friendly' script and the link appears on 1k pages. Weep.

In case anyone asks I have a day job (although I did get a bottle of scotch at xmas) and I haven't done any homework since 1973!

First post - hope this is ok
wfsp
update: tried to make the code clearer


In reply to Why is the browser displaying a content header? by wfsp

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.