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->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
@valuesdoes.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
&dmyis added and then removed. I suspect it’s to prevent empty trailing arguments from being removed bysplit, but a third argument of-1would do that more clearly.