Home: Perl Programming Help: mod_perl:
Extract a cookie value based on a variable?



rrhill
New User

Jul 2, 2007, 5:53 PM


Views: 17254
Extract a cookie value based on a variable?

I can extract a cookie using a literal has key name:

sub get_cookie {
my $r = shift;
my %cookies = CGI::Cookie->parse($r->header_in('Cookie'));
return $cookies{"test"};
}

But I want to extract the cookie using a variable name rather than a literal key name:

sub get_cookie {
my $r = shift;
my %cookies = CGI::Cookie->parse($r->header_in('Cookie'));
return $cookies{$cookie_name};
}

This isn't working.


KevinR
Veteran


Jul 2, 2007, 8:39 PM


Views: 17252
Re: [rrhill] Extract a cookie value based on a variable?

not sure what your question has to do with mod_perl, but your code looks like it should work OK. Try setting the value of $cookie_name to 'test' and see if it works.

sub get_cookie {
my $cookie_name = 'test';
my $r = shift;
my %cookies = CGI::Cookie->parse($r->header_in('Cookie'));
return $cookies{$cookie_name};
}
-------------------------------------------------