In my CGI script, I have a function that prints an HTML page to stdout. The function is imported from a custom module. I want to show the HTTP response headers in the HTML page.

I can easily show the request headers from printing the server environment variables in the HTML, but it is the response headers that ellude me.

I looked at HTTP::Response. But to use HTTP::Response, I would have to initiate a new request. If I make a fresh request in the module and capture the response, then that response would be different from the one involving the HTML page.

Note: any code would be in my module and not in the CGI script. Don't try to solve this in the CGI script (the caller's namespace). For example:

Package My::Module; require Exporter; @My::Module::ISA = qw(Exporter); @My::Module::EXPORT = qw(the-function); sub the-function { print "Content-type: text/html\n\n"; print <<HTML; Here are the HTTP REQUEST headers: <p> HTTP Request: $ENV{'REQUEST_METHOD'} $0 $ENV{'SERVER_PROTOCOL'} <br> Host: $ENV{'HTTP_HOST'} <br> User-Agent: $ENV{'HTTP_USER_AGENT'} <br> Accept: $ENV{'HTTP_ACCEPT_ENCODING'} <br> Accept-Language: $ENV{'HTTP_ACCEPT_LANGUAGE'} <br> Accept-Charset: $ENV{'HTTP_ACCEPT_CHARSET'} <br> Keep-Alive: $ENV{'HTTP_KEEP_ALIVE'} <br> Connection: $ENV{'HTTP_CONNECTION'} <p> SORRY -- I don't know how to show you the HTTP --RESPONSE-- Headers . +. . HTML } 1; Package Main; #!/usr/bin/perl use My::Module; the-function(); __END__
I hope the code makes my question clearer.

In reply to Showing HTTP response headers by jacques

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.