If you print headers in several places (eg, depending on action to take), you might want to set up a couple of subs to keep your vars nice and local, eg:
#!/usr/bin/perl -w use strict; use CGI; my $q = CGI->new(); # create cookie my $cookie1 = $q->cookie(-name=>'sessionID', -value=>'xyzzy', -expires=>'+1h', -path=>'/'); # set cookie set_cookie($cookie1); # create another cookie my $cookie2 = $q->cookie(-name=>'another_one', -value=>'abbaaba', -expires=>'+1h', -path=>'/'); # set other cookie set_cookie($cookie2); # or, set both cookies at the same time set_cookie($cookie1,$cookie2); print cookie_header(), $q->start_html, $q->p('hello'), $q->end_html; exit(0); # this is the localized 'cookie jar' :) { my @cookies = (); sub set_cookie { push @cookies, @_; } sub cookie_header { $q->header(-cookie=>[@cookies]); } }
And if there's no cookies to be set, no problem as far as the script is concerned - so just replace $q->header with cookie_header() throughout and you can stop worrying.

.02

cLive ;-)

--
seek(JOB,$$LA,0);


In reply to (cLive ;-) Re: Header Visible In CGI output after cookie header is sent by cLive ;-)
in thread Header Visible In CGI output after cookie header is sent by jerrygarciuh

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.