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

I've been thrashing around the monastery (and Google, and a few other programing sites) for a few days trying to figure out why my code for setting a cookie works in one cgi script but not another. This one works (i.e. it sets the cookie):
# Process form and set cookies my ($cat) = param('butCat'); my ($typ) = param('txtTyp'); my ($to_set) = cookie(-name => 'cat', -value => $cat, -path => '/', ); my ($to_set_a) = cookie(-name => 'bus_com', -value => $typ, -path => '/', ); print header(-cookie => [$to_set, $to_set_a]);
This one doesn't (i.e. no cookie):
$i = 0; while (@record[$i]){ # Set adv_cookie $cgi = CGI->new; $adv = @record[$i]; my ($to_set) = cookie( -name => 'adv', -value => $adv, -path => '/', ); print header(-cookie=>[$to_set]); $i=$i+1; my (@args) = ($path."advlst.cgi"); system(@args);
I have checked for the cookie manually on the browser as well as using cookie('adv') in the script called using system. I'm picking that I've done something really simple wrong but I can't for the life of me see how this is different from a dozen or so scripts I have seen online (or from my first script). Any suggestions?

Replies are listed 'Best First'.
Re: Another cookie setting question
by wfsp (Abbot) on Oct 11, 2006 at 08:31 UTC
    In the second snippet you're using the 'object-oriented form' of CGI.pm. So you need to use:
    my ($to_set) = $cgi->cookie(...); # and later print $cgi->header(...);
    See the docs . Look at the bottom of the section 'HTTP Cookies'.

    Update: Pointed the link to HTTP COOKIES.

Re: Another cookie setting question
by lima1 (Curate) on Oct 11, 2006 at 08:32 UTC
    Don't know whether it is the problem, but in the second snipplet, you are mixing OO and function-oriented style (and btw, it makes no sense to contruct the $cgi object in the loop). In OO style, you have to use
    $cgi->cookie( ... )
    How do you load CGI.pm? If you have no idea what I am talking about, please read CGI
      Ok, I changed the second piece of code to all be Object Oriented but no luck. For now I've just done a work around that is more at my level of understanding. (No cookies.)
      Fair comment about reading the docs too. I've just been skimming them up until now. I've printed them out today - some light bedtime reading.
      Thanks for your input.