http://www.perlmonks.org?node_id=852172

FRIENDOFGOD has asked for the wisdom of the Perl Monks concerning the following question:

How do I check if a header has been defined.

For example, IF a Module had

print "content-type: text-html\n\n";

in the Module, and so did the Page that used the Module, then the Extra "content-type: text/html" would display in the Browser.

How do I check if the Header has been defined to avoid printing the Header twice, something such as:

unless(header){print "content-type: text/html\n\n";}

And more generally, how do I access all the Headers that were printed?

- Look For God

Replies are listed 'Best First'.
Re: CHECKING IF HEADER DEFINED
by GrandFather (Sage) on Jul 30, 2010 at 21:54 UTC

    Most CGI related modules print their output so you could:

    my $savedOutput; open my $saveOutput, '>', \$savedOutput; my $oldStdOut = select $saveOutput; # Call CGI stuff here select $oldStdOut; close $saveOutput; # process $savedOutput to clean up duplicate headers and other foibles + here print $savedOutput; # Output the cleaned up CGI output text
    True laziness is hard work
Re: CHECKING IF HEADER DEFINED
by Corion (Pope) on Jul 30, 2010 at 21:47 UTC

    You can't know what headers have been printed.

    You need to keep track of all headers yourself.

    Also, there is no need to yell.

      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: CHECKING IF HEADER DEFINED
by planetscape (Chancellor) on Jul 31, 2010 at 00:50 UTC
    Look For God

    Is She lost?

    HTH,

    planetscape
      Cthulu may wear makeup, but that doesn't make him female.
Re: CHECKING IF HEADER DEFINED
by trwww (Priest) on Jul 30, 2010 at 22:50 UTC

    It looks to me like you are using a poorly engineered home grown web app framework and you've got it bursting at the seams.

    You should check out CGI::Application, a whole host of problems like this one simply disappear.

      And after checking out CGI::Application, you should choose not to use it. It's just a way of replacing a long if/elsif/elsif/else statement with some different and equally verbose boilerplate. IMO it's pointless.
        It's just a way of replacing a long if/elsif/elsif/else statement with some different and equally verbose boilerplate. IMO it's pointless.

        Nope, CGI::Application is a way of modularizing your code by splitting it up into screens (one sub per screen/action).

        Not having to write a giant if/elsif/elsif/else to separate your screens is a huge timesaver.

        Very pointy

Re: CHECKING IF HEADER DEFINED
by Your Mother (Bishop) on Jul 30, 2010 at 23:32 UTC

    This is a possibility in some (sloppily/disparately coded) cases but only if you're using CGI to do your headers already.

    use CGI; $CGI::HEADERS_ONCE = 1;

    (Sidebar: your user name is, of course, up to you but please change your sig block. This is not a place of worship for anything but Perl.)

      This being a Perl Monastery the bible is The Camel Book so the path to Godliness being sought is surely perfection on Perl coding. Bearing that in mind the OP's sig is most apposite and to be lauded. He is obviously shouting for the sake of the pure joy in his journey. ;-)

      True laziness is hard work