Closed p5pRT closed 21 years ago
I tried to retrieve a cookie from the hash returned from the CGI:Cookie module using the following code: --BEGIN CODE-- use constant AUTHENTCOOKIE => 'foo'; \<---SNIP--> my %cookies = CGI::Cookie->parse($request->header_in('Cookie')); my $cookie = $cookies{AUTHENTCOOKIE}; --END CODE-- but never could get a value for $cookie. It was undefined\, even though there was a value referenced by AUTHENTCOOKIE in the hash. Eventually I had to replace the above code with the following: --BEGIN CODE-- use constant AUTHENTCOOKIE => 'foo'; \<--SNIP--> my %cookies = CGI::Cookie->parse($request->header_in('Cookie')); my $cookie; foreach (keys %cookies){ if($_ eq AUTHENTCOOKIE){ $cookie = $cookies{$_}; } } --END CODE-- which gave me a proper value for $cookie. I can't explain the behavior\, but I have been able to reproduce it in a smaller program: --BEGIN CODE-- #!/usr/bin/perl
use CGI::Cookie;
use constant NAME => 'crazystuff';
$cookie = CGI::Cookie->new(-name => NAME\, -value => {foo => 'FOO'\, bar => 'BAR'\, mash => 'MASH'}\, -path => '/'\, -expires => '+1d'); %cookies = ( 'crazystuff' => $cookie);
$testcookie = $cookies{NAME}; print "cookie = $cookie. ref(cookie) = " . ref($cookie) . "\n"; print "testcookie = $testcookie. ref(testcookie) = " . ref($testcookie) . "\n";
foreach (keys %cookies){ print "foreach:%cookies{$_} = $cookies{$_}\, ref = " . ref($cookies{$_}) . "\n"; }
print "scalar: cookie = $cookies{'crazystuff'}. ref = " . ref($cookies{'crazystuff'}) . "\n"; --END CODE-- This should output the following since 'crazystuff' eq NAME:
cookie = crazystuff=mash&MASH&foo&FOO&bar&BAR; path=/; expires=Thu\, 10-Aug-2000 01 :44:59 GMT. ref(cookie) = CGI::Cookie testcookie = crazystuff=mash&MASH&foo&FOO&bar&BAR; path=/; expires=Thu\, 10-Aug-200 0 01:46:02 GMT. ref(testcookie) = CGI::Cookie foreach:%cookies{crazystuff} = crazystuff=mash&MASH&foo&FOO&bar&BAR; path=/; expir es=Thu\, 10-Aug-2000 01:45:35 GMT\, ref = CGI::Cookie scalar: cookie = crazystuff=mash&MASH&foo&FOO&bar&BAR; path=/; expires=Thu\, 10-Aug -2000 01:45:35 GMT. ref = CGI::Cookie
It does not however. If configured as above\, it outputs:
cookie = crazystuff=mash&MASH&foo&FOO&bar&BAR; path=/; expires=Thu\, 10-Aug-2000 01 :50:03 GMT. ref(cookie) = CGI::Cookie testcookie = . ref(testcookie) = foreach:%cookies{crazystuff} = crazystuff=mash&MASH&foo&FOO&bar&BAR; path=/; expir es=Thu\, 10-Aug-2000 01:50:03 GMT\, ref = CGI::Cookie scalar: cookie = crazystuff=mash&MASH&foo&FOO&bar&BAR; path=/; expires=Thu\, 10-Aug -2000 01:50:03 GMT. ref = CGI::Cookie
It is missing the testcookie definition. If configured so that %cookies = (NAME => $cookie);\, it returns:
cookie = crazystuff=mash&MASH&foo&FOO&bar&BAR; path=/; expires=Thu\, 10-Aug-2000 01 :46:02 GMT. ref(cookie) = CGI::Cookie testcookie = crazystuff=mash&MASH&foo&FOO&bar&BAR; path=/; expires=Thu\, 10-Aug-200 0 01:46:02 GMT. ref(testcookie) = CGI::Cookie foreach:%cookies{NAME} = crazystuff=mash&MASH&foo&FOO&bar&BAR; path=/; expires=Thu \, 10-Aug-2000 01:46:02 GMT\, ref = CGI::Cookie scalar: cookie = . ref =
It is missing the scalar definition.
My analysis therefore is that there is a bug in the hash derefrencing operator which causes a static tick (') delimited string to not equal a constant string. This gives a null reference when trying to dereference by the value not used to create the hash.
On Tue\, Aug 08\, 2000 at 10:01:51PM -0400\, Joseph Tate wrote:
I tried to retrieve a cookie from the hash returned from the CGI:Cookie module using the following code: --BEGIN CODE-- use constant AUTHENTCOOKIE => 'foo'; \<---SNIP--> my %cookies = CGI::Cookie->parse($request->header_in('Cookie')); my $cookie = $cookies{AUTHENTCOOKIE}; --END CODE-- but never could get a value for $cookie. It was undefined\, even though there was a value referenced by AUTHENTCOOKIE in the hash.
$cookies{AUTHENTCOOKIE} looks up the key 'AUTHENTCOOKIE'\, not the key 'foo'. If you want to use a function call in a hash index\, you have to make it an expression\, instead of a bareword. For example: $cookies{+AUTHENTCOOKIE}
Ronald
Migrated from rt.perl.org#3659 (status was 'resolved')
Searchable as RT3659$