in reply to Did I already print a header?

Perhaps you may want to use CGI::Carp, which will print out a minimal header if you don't have one already. A code snippet taken directly from perlman:CGI::Carp that might be applicable is:
use CGI::Carp qw(fatalsToBrowser set_message); BEGIN { sub handle_errors { my $msg = shift; print "<h1>Oh gosh</h1>"; print "Got an error: $msg"; } set_message(\&handle_errors); }
It looks like you could put your info from caller and DBI in the handle_errors subroutine, and then die works normal, no need to put it in a wrapper.

=Blue
...you might be eaten by a grue...

Replies are listed 'Best First'.
Re: Re: Did I already print a header?
by merlyn (Sage) on Nov 30, 2000 at 02:37 UTC
    CGI::Carp has no idea if a header has been printed already. It always prints a new header.

    As far as I know, there's no way to tell if something has already been sent down standard output. You'll just need to have things which cooperate. {grin}

    -- Randal L. Schwartz, Perl hacker

      Tom Christensen offers a good way to filter in the Perl Cookbook in chapter 16.5 (Filtering Your Own Output) by forking open on your own STDOUT and then having the child process filter STDIN to STDOUT.

      #!/usr/bin/perl -w use strict; use CGI; oneheader(); my $q = new CGI; print $q->header; print $q->header; close(STDOUT); exit; sub oneheader { my $pid; my $seen = 0; return if $pid = open(STDOUT, "|-"); die "cannot fork: $!" unless defined $pid; while(<STDIN>) { if(m/^Content-Type/) { print unless $seen; $seen = 1; } else { print; } } exit; }
      Which produces:
      (offline mode: enter name=value pairs on standard input)
      Content-Type: text/html
      
      
      
      (yep, thats two blank lines)

      Another method is to tie a file handle and select it. This requires that you play nice and don't try to fool it by printing something in multiple chunks. This will also fail if CGI::Carp dosn't print to the selected filehandle but rather STDOUT directly. Please forgive me for not doing the return value from PRINT nicely and failing to implement PRINTF and WRITE.

      #!/usr/bin/perl -w use strict; use CGI; tie *FILTER, "OneHeader"; my $q = new CGI; select FILTER; print $q->header; print $q->header; print "Foo!\n"; exit; package OneHeader; sub TIEHANDLE { my $class = shift; my $me = 0; bless \$me, $class; } sub PRINT { my $self = shift; foreach my $item (@_) { if($item =~ m/^Content-Type/) { if(not $$self) { $$self = 1; print STDOUT $item; } } else { print STDOUT $item; } } 1; } 1;

      The output of this method is:

      (offline mode: enter name=value pairs on standard input)
      Content-Type: text/html
      
      Foo!
      

      Just looked at the code for CGI::Carp and it always specifies the filehandle that it prints to. Thus the tie method will require additional twists to get it to work. Still, the essence is there.