Fellow monasterians,

I've managed to survive many scenarios where I've had to write multiple CGI headers in a script, but it always feels like I'm hunting gophers--as soon as I kill one, another pops up. And now I'd like to settle the issue once and for all. Note: In all fairness, I did ask a related question, but that was concerned more with running other Perl programs.

Anyway, I use a combination of HTML::Template and CGI to populate my pages, write cookies, and to redirect. Of course, all these require headers. Sure it would be convenient to group them and only write it once, but in larger programs, where there can be many conditions, it's often just not possible. Here is a stripped down version of a larger application that highlights the problem:

use CGI; my $query = new CGI; if ($somecondition) { cookie(); } if ($anothercondition) { menu(); } else { my $template = HTML::Template->new(filename => 'some.tmpl'); $template -> param(message => "Hello World"); print "Content-type: text/html\n\n"; print $template->output(); } exit(0); sub cookie { my $newcookie = $query->cookie( -name => 'sessionID', -value => "test", -expires => '+60m'); print $query->header(-cookie => $newcookie); } sub menu { print $query->redirect('http://www.somedomain.org/index.html'); }

In some cases I only need to print one, other times all three. Is there an alternative to writing the bloated code to jump through all the hoops to eliminate the problems that arise, e.g., the 302 error. Thanks!

Update 1: I think I confused the issue with my code. It boils down to, how do I 1) print a cookie, and 2) redirect in the "same breath?"

print $query->header(-cookie => $newcookie); print $query->redirect('http://www.somedomain.org/index.html');

Update 2: FWIW, I have finally managed to write the cookie and redirect at the same time, but without using CGI conventions. Sadly. But it works and I'm back on track.

print "Set-Cookie: $cookie\n"; print "Location: $path\n\n";

Update 3: Smylers came up with the CGI solution I was looking for. Thanks.


—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

In reply to The problems of multiple CGI headers by bradcathey

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.