About the way CGI::Cookie parses cookie
sub parse {
my ($self,$raw_cookie) = @_;
my %results;
my @pairs = split("[;,] ?",$raw_cookie);
foreach (@pairs) {
s/\s*(.*?)\s*/$1/;
my($key,$value) = split("=",$_,2);
# Some foreign cookies are not in name=value format, so ignore
# them.
next if !defined($value);
my @values = ();
if ($value ne '') {
@values = map unescape($_),split(/[&;]/,$value.'&dmy');
pop @values;
}
$key = unescape($key);
# A bug in Netscape can cause several cookies with same name to
# appear. The FIRST one in HTTP_COOKIE is the most recent version.
$results{$key} ||= $self-&g开发者_开发技巧t;new(-name=>$key,-value=>\@values);
}
return \%results unless wantarray;
return %results;
}
Anyone knows why @values = map unescape($_),split(/[&;]/,$value.'&dmy');
is there?
Why must convert it to an array first?
Converted? First? Your question doesn't make much sense. I'm going to assume you want to know what the code that populates @values
does.
The code in question takes a string containing a series of values, and separates it into a list of values. This list is stored in an array because it's the only type of variable that would hold that.
I'm not clear on why &dmy
is added and then removed. I suspect it's to prevent empty trailing arguments from being removed by split
, but a third argument of -1
would do that more clearly.
A cookie can have multiple fields, and each field can have multiple values. The delimiters used are either semi-colons and ampersands:
time=12345678; items=123&456&789; other=abc
or alternatively commas and semicolons:
time=12345678, items=123;456;789, other=abc
Note that in the second case a space may not follow the semi-colon - otherwise it will be interpreted as field assignment delimiter.
So, to answer your question, it converts to an array because a cookie field can have multiple values.
精彩评论