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

I have been able to set cookies using Perl but am unable to retrieve all of the values I've set. i've parsed them but i can't seem to get them all out of the hash.

here's the code in question:

&setThem($username,$logon,$hcolor,$sig); &getThem; print "rname=$rname logon=$logon hcolor=$hcolor sig=$sig\n"; sub setThem { # ---------------------------------- print "Set-Cookie:rname=$_[0]\n"; print "Set-Cookie:logon=$_[1]\n"; print "Set-Cookie:hcolor=$_[2]\n"; print "Set-Cookie:sig=$_[3]\n"; } sub getThem { # ---------------------------------- @cookies = split (/;/, $ENV{'HTTP_COOKIE'}); foreach $cookie (@cookies) { ($name, $value) = split (/=/, $cookie); $crumbs{$name} = $value; } $rname = $crumbs{'rname'}; $logon = $crumbs{'logon'}; $hcolor = $crumbs{'hcolor'}; $sig = $crumbs{'sig'}; }
when the "getThem" sub is called, only the "$rname" will print. all the others will not. I know that they're set in the cookie but am unable to pull them from the hash.

any thought? thanks in advance.

grahm

Edit kudra, 2001-10-05 Changed title

Replies are listed 'Best First'.
Re: from a newbie...
by dondelelcaro (Monk) on Oct 05, 2001 at 02:41 UTC
    Couple of things:
    1. #! /usr/bin/perl -wT use strict;
      is your friend.
    2. You're using global variables $rname etc when you should probably be using local variables. That's just good coding technique.
    3. You probably should be implementing cookies using CGI instead of hand rolling it, at least initially. (As an exercise is cool of course...)
    Now, I'm not entirely sure exactly how $ENV->{'HTTP_COOKIE'} gets set, but I would imagine it wont be set until your cgi is called the second time around. (Someone correct me if I'm wrong.) In the meantime, why don't you try printing the contents of $ENV->{'HTTP_COOKIE'} to stderr to see if it's actually set?
Supper time around the CGI campfire.
by boo_radley (Parson) on Oct 05, 2001 at 02:38 UTC
    here's the best free advice you'll ever get : use CGI or die;

    It does everything you're looking for, and much more. Save time. Save effort. Save yourself : use CGI or die;.