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

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

Replies are listed 'Best First'.
Re: The problems of multiple CGI headers
by Joost (Canon) on Apr 16, 2005 at 19:08 UTC
Re: The problems of multiple CGI headers
by Thelonious (Scribe) on Apr 17, 2005 at 10:33 UTC
    I'm not 100% sure what you're asking, but I like to do something like this:

    use CGI ':standard'; my @cookies = (); push @cookies, cookie() if $test; # etc... test2() ? redirect($url) : header(-cookie => \@cookies); # go on to create the page.

    It seems straightforward to me to build the header list as I go, then print the content after the headers are all printed. If I don't maintain a firewall (so to speak) between the two stages of execution, it can often get very hairy very quickly!

    That said, I think that it's a good idea to use an existing and well-tested framework instead of writing everything from scratch every time. Personally, I like HTML::Mason. It's a breeze compared with writing vanilla CGI scripts.

Re: The problems of multiple CGI headers
by Smylers (Pilgrim) on Apr 18, 2005 at 09:10 UTC
    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?" ... I have finally managed to write the cookie and redirect at the same time, but without using CGI conventions.

    You can pass the cookie to the redirect method; redirect is really just a specialized version of header:

    print $query->redirect('http://www.example.org/', -cookie => $newcooki +e);

    Though now I look, I can't actually spot where this is documented.

    Smylers

      That was it! Worked perfectly.

      I was close. I had tried the very strange looking:

      print $query->header(-redirect=>'http://www.domain.com', -cookie=>$coo +kie);

      but to no avail. Thanks.


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