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.


In reply to Re: Re: Re: Did I already print a header? by m_turner
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.