If you're using the object-oriented version of CGI.pm, you can set $CGI::HEADERS_ONCE++ before instantiating the CGI object to suppress multiple headers. Then, just print headers with impunity (with the caveat that if you are passing cookies or redirect headers or other things like that which could conceivably change user's session, you'll lose them if they're not printed in the first header). This may work with the function oriented version, but I haven't tested it.

The only way I know of to actually find out if the header has been printed to to examine the CGI object itself.

$ perl -MData::Dumper -e "use CGI; my $q = CGI->new;print $q->header; +print Dumper($q)" Content-Type: text/html; charset=ISO-8859-1 $VAR1 = bless( { '.header_printed' => '1', '.charset' => 'ISO-8859-1', '.parameters' => [], '.fieldnames' => {} }, 'CGI' );
As you can see, the object tracks if the header has been printed, but that's not a good thing to rely on as you shouldn't be messing with the internals. The following will test for a printed header:
use CGI; my $q = new CGI; # do a whole bunch of stuff print "header printed!" if exists $q->{'.header_printed'};
Again, that will work, but I don't recommend it. If anyone knows if CGI has a direct method for accessing that information, chime in!

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid) Re: Did I already print a header? by Ovid
in thread Did I already print a header? by jptxs

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.