in reply to Did I already print a header?

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.

Replies are listed 'Best First'.
Re: (Ovid) Re: (jptxs) Did I already print a header?
by jptxs (Curate) on Nov 30, 2000 at 01:03 UTC

    Any idea where I can find doc on this feature? Searching through perldoc and I don't see it anywhere...nor does /HEADERS

    "A man's maturity -- consists in having found again the seriousness on +e had as a child, at play." --Nietzsch +e
      As far as I know, this is not documented in Perldoc or in the CGI POD. In fact, I don't know how I found out about it. It's just something I picked up. You can see how it works by examing the CGI.pm source code, but bring along some aspirin :)

      I even went out to Lincoln Stein's home page and I can't find a reference there in his official docs. Sorry :(

      Cheers,
      Ovid

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