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

Fellow monks, I making a first attempt at setting and reading cookies. I seem to be able to set a cookie successfully:
my $r = Apache->request; Apache::Cookie->new( $r, name => 'license_session', value => $secret, path => '/', expires => '+8h', )->bake;
My real problem is in reading cookies:
my $cookie = Apache::Cookie->fetch; my $name = $cookie->name; my $value = $cookie->value; print "<h3>name = $name</h3>\n"; print "<h3>value = $value</h3>\n";

An error is returned: Can't call method "name" on an undefined value. This error points to the line where $name is defined. The documentation for apache::cookie is pretty small. Having read it, I thought this would be simple. What have I missed?

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: Help with Apache::Cookie
by arthas (Hermit) on Nov 02, 2003 at 21:18 UTC

    Hi!

    Apache::Cookie->fetch() returns an hash reference, not a Cookie object. The keys of the hash are the cookie names, while the values are the actual objects.

    Hope this helps!

    Michele.

      I still don't know what the syntax is supposed to be :(

      Neil Watson
      watson-wilson.ca

Re: Help with Apache::Cookie
by neilwatson (Priest) on Nov 03, 2003 at 18:00 UTC
    Success. Here is how to retreave a cookie:

    my $cookies = Apache::Cookie->fetch(); my ($name, $value); # 'license_session' is the name of the cookie # I am looking for. if ($cookies->{license_session}) { $name = $cookies->{license_session}->name(); $value = $cookies->{license_session}->value(); } print "<h3>name = $name</h3>\n"; print "<h3>value = $value</h3>\n";

    Neil Watson
    watson-wilson.ca